ggplot:以相反的顺序堆叠的barplot [英] ggplot: stacked barplot in reverse order

查看:320
本文介绍了ggplot:以相反的顺序堆叠的barplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有数据框架

$ $ p $ $ $ $ $ $ dput(df)
structure(list(Frequency = structure(c( 1L,2L,3L,4L,1L,2L,
3L,4L),.Label = c(2,3,4,5),class =factor prcentage = c(1,
33,58,8,2,40,53,5),label = list(Insufficient,Average,
Good,Excellent,不足,平均,良好,优秀),
name = c(实施,实施,实施,
实施,能源,能源,energy,energy)),.Names = c(Frequency,
Prcentage,label,name),row.names = c(NA,8L) =data.frame)

以下代码

 #以所需顺序获取类型的级别
df $ label = factor(df $ label,levels = c(Unacceptable,Insufficient, 平均,良好,优秀))
df = arrange(df,name,desc(label))

#格式化标签并计算它们的位置
df = ddply(df,。(name),transform,pos = (cumsum(Prcentage) - 0.5 * Prcentage))
df $ label1 = paste0(sprintf(%。0f,df $ Prcentage),%)


#plot
ggplot(df,aes(x = factor(name),y = PRcentage,fill = label,order = desc(label)))+
geom_bar(stat =identity,width = 0.5)+
geom_text(aes(y = pos,label = label1),size = 4)+ theme_classic()+
scale_y_continuous(position =top,expand = c(0,0), (0,102),limits = c(0,102),labels = dollar_format(suffix =%,prefix =))+
coord_flip() +
xlab()+ ylab()+
theme(legend.position =bottom,legend.title = element_blank())+
scale_fill_manual(values = c( #ff0000,#fff68f,#b2b2b2,#1baf05,#006080),drop = FALSE)

我产生如下图:



但是现在我正在努力以相反的顺序获得酒吧。 我的输出应当反转堆叠,并且条上的值正确(例如,1%的黄色应该先位于图的左侧,然后是33%,然后是56%,最右侧是8% )。我已经尝试添加

  + geom_col(position = position_stack(reverse = TRUE))(在geom_bar之后) 

哪个产生了这个





但是这个不正确,因为酒吧里的数值是不正确的。



我也看过这里




在ggplot2中为直方图条填写订单



订单S在ggplot中添加条形图



ggplot2中直方图条的反向填充顺序

解决方案

标签的位置是直接由 pos 值设置的,您需要反转f以反转堆栈顺序:

  ggplot(df,aes(x = factor(name)))+ 
geom_col(aes(y = PRcentage,fill = label),
position = position_stack(reverse = TRUE ),
width = .5)+
#将位置设置为其补充
geom_text(aes(y = 100 - pos,label = label1))+

#其余主题
coord_flip()+
scale_y_continuous(position =top,
expand = c(0,0),
breaks = seq(min(0) ,max(0,102),by = 10),
limits = c(0,102),
labels = dollar_format(suff ix =%,prefix =))+
scale_fill_manual(values = c(#ff0000,#fff68f,#b2b2b2,#1baf05,#006080), = false)+
xlab()+ ylab()+
theme_classic()+
theme(legend.position =bottom,legend.title = element_blank())


So I have data frame

dput(df)
structure(list(Frequency = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 
3L, 4L), .Label = c("2", "3", "4", "5"), class = "factor"), Prcentage = c(1, 
33, 58, 8, 2, 40, 53, 5), label = list("Insufficient", "Average", 
    "Good", "Excellent", "Insufficient", "Average", "Good", "Excellent"), 
    name = c("implementation", "implementation", "implementation", 
    "implementation", "energy", "energy", "energy", "energy")), .Names = c("Frequency", 
"Prcentage", "label", "name"), row.names = c(NA, 8L), class = "data.frame")

And with following code

# Get the levels for type in the required order
df$label = factor(df$label, levels = c("Unacceptable","Insufficient", "Average","Good","Excellent"))
df = arrange(df, name, desc(label))

# Format the labels and calculate their positions
df = ddply(df, .(name), transform, pos = (cumsum(Prcentage) - 0.5 * Prcentage))
df$label1 = paste0(sprintf("%.0f", df$Prcentage), "%")


# Plot
ggplot(df, aes(x = factor(name), y = Prcentage, fill = label, order=desc(label))) +
  geom_bar(stat = "identity", width = 0.5) +
  geom_text(aes(y = pos, label = label1), size = 4) +  theme_classic() + 
  scale_y_continuous(position = "top",expand = c(0, 0),breaks = seq(min(0), max(0,102), by = 10),limits = c(0,102),labels = dollar_format(suffix = "%", prefix = "")) + 
  coord_flip() +
  xlab("") + ylab("") + 
  theme(legend.position="bottom",legend.title = element_blank()) +
  scale_fill_manual(values = c("#ff0000","#fff68f","#b2b2b2","#1baf05","#006080"),drop = FALSE) 

I produce the following plot

But now I am struggling to get the bars in reverse order. Sm my output should be reverse stacked with the right values in bars (e.g. 1% yellow should be positioned first in the left side of the plot, then follows 33%, then 56% and far right 8%). I've already tried to do this with adding

+ geom_col(position = position_stack(reverse = TRUE)) (after geom_bar)

Which produced this

But this in not correct as values in bars are not correct.

I've also looked here

How to control ordering of stacked bar chart using identity on ggplot2

Reverse fill order for histogram bars in ggplot2

Order Stacked Bar Graph in ggplot

Reverse fill order for histogram bars in ggplot2

解决方案

The position of the labels is directly set by the pos value, you need to reverse that f you reverse the stack order:

ggplot(df, aes(x = factor(name))) +
  geom_col(aes(y = Prcentage, fill = label), 
           position = position_stack(reverse = TRUE),
           width = .5) +
  # Set the position to its complementary
  geom_text(aes(y = 100 - pos, label = label1)) +

  # Rest of theme
  coord_flip() +
  scale_y_continuous(position = "top", 
                     expand = c(0, 0),
                     breaks = seq(min(0), max(0,102), by = 10),
                     limits = c(0,102),
                     labels = dollar_format(suffix = "%", prefix = "")) + 
  scale_fill_manual(values = c("#ff0000","#fff68f","#b2b2b2","#1baf05","#006080"), drop = FALSE) +
  xlab("") + ylab("") + 
  theme_classic() +
  theme(legend.position="bottom",legend.title = element_blank())

这篇关于ggplot:以相反的顺序堆叠的barplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆