How to change the y axis to display percent (%) in Python Plotnine barplot?
The labels
parameter accepts a callable that takes the list of break points as input. All you have to do is to convert each item in the list manually:
scale_y_continuous(labels=lambda l: ["%d%%" % (v * 100) for v in l])
Calculating with y-axis labels of stacked bar plot (either *4 or into percent)
You could add this to your code:
scale_y_continuous(labels = function(x) paste0((x/max(x))*100, "%"))
For the given example dataset without(event_labels
):
How to change y-axis to percentages with ggplot2 in R?
Use function scale_y_continuous()
. It have an argument labels
. You can provide any function to this argument to convert labels you have to proper ones.
In your case if you want to map interval [-2;2] to [-0.1; 0.1] what you could do:
p <- ggplot(...) + geom_*(...)
p + scale_y_continuous(labels = function(x) paste0(x/20, "%"))
So this function gets each numeric label, divides it by 20 and convert into character.
Hope that's what you are looking for.
ggplot barplot - percentage on y axis
I think you just got your melt
wrong.
Try
mdat <- melt(dset.bnull[c(1:2, 5:7)], id.vars=c("id", "group"))
and remove
position=position_dodge(1)
from the geom_bar
call.
Update: From your comment I'm guessing you're comfortable using dplyr
. It'll be more intuitive to plot after summarising the data.
plot_data = mdat %>% group_by(group, variable) %>% summarise(total = sum(value))
ggplot(plot_data, aes(fill = group)) +
geom_bar(aes(x = variable,
y = (total)/sum(total)),
stat = "identity",
position = "dodge") +
scale_y_continuous(labels = percent) +
xlab("Referent") +
ylab("Percentage of Answers") +
guides(fill=guide_legend(title="Experimental \nGroup"))
How to change y axis from count to prop?
Try this:
ggplot(mtcars, aes(x = cyl)) +
geom_bar(aes(y = ..prop..), fill = "steelblue", stat = "count") +
geom_text(aes(label = scales::percent(..prop..), y = ..prop.. ), stat= "count", vjust = -.5) +
ylim(0, 0.5) +
ylab("") +
theme_minimal() +
theme(aspect.ratio = 1.5)
Edit: if you want a factor on x
axis try
ggplot(mtcars, aes(x = factor(cyl))) +
geom_bar(aes(y = (..count..)/sum(..count..)), fill = "steelblue", stat = "count") +
geom_text(aes(label = scales::percent(round((..count..)/sum(..count..), 2)),
y = ((..count..)/sum(..count..))), stat = "count", vjust = -.25) +
ylim(0, 0.5) +
ylab("") +
theme_minimal() +
theme(aspect.ratio = 1.5)
Edit2: with the GGally
package you can use:
ggplot(mtcars, aes(x = factor(cyl), by = 1)) +
geom_bar(aes(y = ..prop..), fill = "steelblue", stat = "prop") +
geom_text(aes(label = scales::percent(..prop..), y = ..prop.. ), stat = "prop", vjust = -.5) +
ylim(0, 0.5) +
ylab("") +
theme_minimal() +
theme(aspect.ratio = 1.5)
Format y axis as percent
This is a few months late, but I have created PR#6251 with matplotlib to add a new PercentFormatter
class. With this class you just need one line to reformat your axis (two if you count the import of matplotlib.ticker
):
import ...
import matplotlib.ticker as mtick
ax = df['myvar'].plot(kind='bar')
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
PercentFormatter()
accepts three arguments, xmax
, decimals
, symbol
. xmax
allows you to set the value that corresponds to 100% on the axis. This is nice if you have data from 0.0 to 1.0 and you want to display it from 0% to 100%. Just do PercentFormatter(1.0)
.
The other two parameters allow you to set the number of digits after the decimal point and the symbol. They default to None
and '%'
, respectively. decimals=None
will automatically set the number of decimal points based on how much of the axes you are showing.
Update
PercentFormatter
was introduced into Matplotlib proper in version 2.1.0.
Displaying percentages on Y-axis
My previous answer was a bit messy given edits.
Here is a fresh self-contained answer based on nicelabels
(on SSC since 10 May 2022) and mylabels
(on SSC for some while, perhaps 2003).
Let's start by noting that adding % signs is not part of any official display format. So, we have to do it in our own code.
clear
input yr v1
2005 77.01
2006 84.01
2007 83.01
2008 85.01
2009 86.01
2010 83.01
2011 98.01
2012 80.01
2013 79.01
end
nicelabels v1, local(yla)
if wordcount("`yla'") < 5 nicelabels v1, local(yla) nvals(10)
mylabels `yla', suffix(%) local(yla)
twoway connected v1 yr , yla(`yla')
So nicelabels
is asked to suggest nice labels for v1
. If the number suggested is < 5 it is told to try again. Once those labels exist, they are pushed through mylabels
for adding %
to each. The process needs no user intervention.
Related Topics
Selecting Only Numeric Columns from a Data Frame
Put Stars on Ggplot Barplots and Boxplots - to Indicate the Level of Significance (P-Value)
Conditionally Change Panel Background With Facet_Grid
Ggplot, Facet, Piechart: Placing Text in the Middle of Pie Chart Slices
Table of Interactions - Case With Pets and Houses
Tools For Making Latex Tables in R
Change the Blank Cells to "Na"
R Spreading Multiple Columns With Tidyr
How to Merge Color, Line Style and Shape Legends in Ggplot
Subscript Letters in Ggplot Axis Label
Split Date-Time Column into Date and Time Variables
What's Wrong With My Function to Load Multiple .Csv Files into Single Dataframe in R Using Rbind
Generate a Sequence of the Last Day of the Month Over Two Years
R Shiny: Handle Action Buttons in Data Table
Data.Table "Key Indices" or "Group Counter"