I'm trying to plot my data as a stacked bar with 3 levels ("catg"), but I want the categories on the X-axis to appeared in increasing order by their value of the "low" sub-categories,
here is reproductive example:
#creating df:
set.seed(33)
df<-data.frame(value=runif(12),
catg=factor(rep(c("high","medium","low")),
levels = c("high","medium","low")),
var_name=(c(rep("question1",3),rep("question2",3),rep("question3",3),rep("question4",3)))
#plotting
bar_dist<-ggplot(df,aes(x=var_name,
y=value,
fill=catg,
label=round(value,2)))
bar_dist+ geom_bar(stat = "identity",
position = "dodge",
width = 0.7)+
coord_flip() +
xlab("questions")+
ylab("y")+
geom_text(size = 4,position=position_dodge(width = 0.7))
And here is my current plot:
so in this case I should have question3 and then 4, 1, and finally 2. every help will be appreciate,
A solution that doesn't modify the data frame, using fct_reorder2()
from the forcats
library:
library(forcats)
bar_dist <- ggplot(df,
aes(
x = fct_reorder2(var_name, catg, value),
y = value, fill = catg,
label = round(value, 2)))
bar_dist + geom_bar(stat = "identity",
position = "dodge",
width = 0.7) +
coord_flip() +
xlab("questions") +
ylab("y") +
geom_text(size = 4, position = position_dodge(width = 0.7))
One way could be :
df$var_name=factor(df$var_name,levels=rev(levels(reorder(df[df$catg=="low",]$var_name,df[df$catg=="low",]$value))))
It uses reorder()
as suggested by Richard Telford to reorder the levels according to df$value
after filtering df
to keep only the "low"
.
levels()
is used to extract the levels from the previous function.
rev()
is used to reverse the levels.
factor()
reassigns the levels to df$var_name
or :
df$var_name=factor(df$var_name,levels = df[with(df,order(value,decreasing = T)) ,][df[with(df,order(value,decreasing = T)) ,]$catg=="low",]$var_name)
It sorts df
on df$value
(by decreasing value), filters on df$catg
for "low"
and retrieves df$var_name
which is used as levels in factor()
.
The same plotting function is then used:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.