Out of order text labels on stack bar plot (ggplot)
ggplot(meanCombM,aes(group,value,label=value)) +
geom_col(aes(fill=variable))+
geom_text(aes(group=variable),position = position_stack(vjust = 0.5))+
coord_flip()
Hadley answered a question similar to my own in this issue in ggplot2's git repository: https://github.com/tidyverse/ggplot2/issues/1972
Apparently the default grouping behaviour (see:
http://ggplot2.tidyverse.org/reference/aes_group_order.html) does not partition the data correctly here without specifying a group
aesthetic, which should map to the same value as fill
in geom_col
in this example.
Label order in stacked bar plot ggplot2
p7<-ggplot(st, aes(x = factor(Question), y = Percentage, fill = Value)) + geom_bar(position = position_stack(), stat = "identity", width = .7) + geom_text(aes(label = label), position = position_stack(vjust = 0.5),size = 4) + coord_flip() + scale_fill_brewer(palette = 18)
p7+labs(x="Question", y="Percentage Values", fill="Legend")+ theme(axis.text=element_text(size=15), axis.title=element_text(size=17, face="bold"), legend.title=element_text(size=13), legend.text=element_text(size=12))
Solved:
R Stacked Bar Chart: How to change the order of geom text labels
You could add position_stack
like this:
library(ggplot2)
ggplot(df_beine_clan, aes(x = Var2, y = Freq, fill = Var1)) +
geom_bar(stat = "identity") +
geom_text(aes(label = Freq), position = position_stack(vjust = 0.5), size = 5)
Created on 2022-09-03 with reprex v2.0.2
Showing data values on stacked bar chart in ggplot2
From ggplot 2.2.0
labels can easily be stacked by using position = position_stack(vjust = 0.5)
in geom_text
.
ggplot(Data, aes(x = Year, y = Frequency, fill = Category, label = Frequency)) +
geom_bar(stat = "identity") +
geom_text(size = 3, position = position_stack(vjust = 0.5))
Also note that "position_stack()
and position_fill()
now stack values in the reverse order of the grouping, which makes the default stack order match the legend."
Answer valid for older versions of ggplot
:
Here is one approach, which calculates the midpoints of the bars.
library(ggplot2)
library(plyr)
# calculate midpoints of bars (simplified using comment by @DWin)
Data <- ddply(Data, .(Year),
transform, pos = cumsum(Frequency) - (0.5 * Frequency)
)
# library(dplyr) ## If using dplyr...
# Data <- group_by(Data,Year) %>%
# mutate(pos = cumsum(Frequency) - (0.5 * Frequency))
# plot bars and add text
p <- ggplot(Data, aes(x = Year, y = Frequency)) +
geom_bar(aes(fill = Category), stat="identity") +
geom_text(aes(label = Frequency, y = pos), size = 3)
labels are incorrectly reversed in stacked bar chart
I would add fill = cust_type
to and remove label = label_txt
from the aesthetics of the ggplot()
. I would add aes(label = label_txt)
to geom_text()
and change reverse = TRUE
to reverse = FALSE
. Then the R-code of your plot should look like the following and hopefully work:
ggplot(
data = df_summ
,aes(
x = group
,y = cust_count
,fill = cust_type)) +
geom_bar(
position = 'stack'
,stat = 'identity') +
geom_text(
aes(label = label_txt)
,size = 4
,position = position_stack(
vjust = 0.5
,reverse = FALSE)) +
scale_fill_manual(
values = setNames(
c('green','beige','salmon')
,types)
,guide = FALSE) +
labs(
x = NULL
,y = NULL
,title = 'Composition of customer base') +
theme_minimal() +
theme(
panel.grid.major = element_blank()
,panel.grid.minor = element_blank()
,axis.text.x = element_blank()
,axis.ticks.x = element_blank()
,axis.text.y = element_blank()
,axis.ticks.y = element_blank());
ggplot2 Stacked Bar Chart Data Labels are Backwards
You'll need to set position to stack for the text labels as well.
ggplot(data_rep, aes(x=Task.Number,y=Perc_SLA, fill=Within_SLA)) +
geom_bar(stat="identity", position="stack") +
geom_text(aes(label=Perc_SLA), position =position_stack(vjust = 0.5)) +
scale_fill_discrete(name = "Within SLA") +
coord_flip()
Manually change order of y axis items on complicated stacked bar chart in ggplot2
I think this works, you may need to play around with the axis limits/breaks:
library(dplyr)
mdfr <- mdfr %>%
mutate(group_n = as.integer(case_when(Group == "Bird Advocates" ~ 2,
Group == "Cat Advocates" ~ 1)))
ggplot(data=mdfr) +
geom_segment(aes(x = group_n, y = start, xend = group_n, yend = start + value, colour = variable,
text=paste("Group: ",Group,"<br>Percent: ",value,"%")), size = 5) +
scale_x_continuous(limits = c(0,3), breaks = c(1, 2), labels = c("Cat", "Bird")) +
geom_hline(yintercept = 0, color =c("#646464")) +
theme(legend.position="top") +
theme(legend.key.width=unit(0.5,"cm")) +
coord_flip() +
guides(col = guide_legend(ncol = 12)) + #has 7 real columns, using to adjust legend position
scale_color_manual("Response", labels = mylevels, values = pal, guide="legend") +
theme(legend.title = element_blank()) +
theme(axis.title.x = element_blank()) +
theme(axis.title.y = element_blank()) +
theme(axis.ticks = element_blank()) +
theme(axis.text.x = element_blank()) +
theme(legend.key = element_rect(fill = "white"))+
scale_y_continuous(breaks=seq(-100,100,100), limits=c(-100,100)) +
theme(panel.background = element_rect(fill = "#ffffff"),
panel.grid.major = element_line(colour = "#CBCBCB"))
produces this plot:
Related Topics
Using Jupyter R Kernel with Visual Studio Code
Multiplying Combinations of a List of Lists in R
How to Print on a Serie Sof Graphs Pairwise Comparisons Bars and Effect Size Value
Dependent Inputs in Shiny Application with R
How to Do a Glm When "Contrasts Can Be Applied Only to Factors with 2 or More Levels"
Total Mean & Mean by Groups in R with Dplyr
How to Determine If a Url Object Returns '404 Not Found'
Converting 1M to 1000000 Elegantly
Aggregating Monthly Column Values into Quarterly Values
How to Embed Plots into a Tab in Rmarkdown in a Procedural Fashion
How to Unlock Environment in R
How to Extend the 'Summary' Function to Include Sd, Kurtosis and Skew
R Replacing Zeros in Dataframe with Next Non Zero Value
Split on Factor, Sapply, and Lm
Pre-Select Rows of a Dynamic Dt in Shiny