简体   繁体   English

条形图 - 堆叠的 ggplot 百分比条形图起始值不是 0%

[英]Barplot - stacked ggplot percentage barplot starting value NOT 0%

I have a plot like this:我有这样的情节:

library(ggplot2)
library(reshape2)
library(ggh4x)

data <- data.frame(col1=c("Sample1","Sample2","Sample3","Sample4","Sample5","Sample6"),
                    col2=c(0.5,0.1,0.4,0.05,0.05,0.9),
                    col3=c(0.6,0.1,0.3,0.1,0.1,0.8),
                    col4=c(0.5,0.3,0.2,0.05,0.15,0.8),
                    col5=c("a","a","a","b","b","b"),
                    col6=c("c","c","c","c","c","c"))

data2 <- melt(data)

ggplot(data=data2, aes(x = variable, y = value, fill=col1))+
  geom_bar(position="stack", stat="identity")+
  scale_fill_manual(values=c("#e6194B","#ffe119","#f58231","#911eb4","#42d4f4","#bfef45")) +
  scale_y_continuous(expand = c(0, 0),labels = scales::percent) +
  facet_nested(~col6 + ~col5, scales = "free_x",space = "free_x",switch = "x") +
  ggtitle("Title") +
  theme_classic() +
  theme(strip.text.y = element_text(angle=0),legend.position = "right",
        legend.key.size = unit(0.4, 'cm'),
        axis.line = element_line(colour = "black"),
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        strip.placement = "outside",                      
        strip.background = element_rect(color = "white", fill = "white"),
        axis.title = element_blank()) +                  
  guides(fill=guide_legend(title=NULL, ncol = 1)) +
  xlab("X axis") +
  ylab("Y axis")

Which creates a barplot like this: Please take a look这会创建一个像这样的条形图:请看一下

My question is simple, how can I set y-axis starting value to 10% instead of 0% (without changing the code too much).我的问题很简单,如何将 y 轴起始值设置为 10% 而不是 0%(无需过多更改代码)。 All answers are greatly appreciated!非常感谢所有答案! (Similar questions are checked, without success...) (类似的问题检查了,没有成功...)

While in general not recommended for bar charts one option to "set" the starting point would be to set the limits via coord_cartesian :虽然通常不建议将条形图用于“设置”一个选项,但起点是通过coord_cartesian设置限制:

library(ggplot2)
library(ggh4x)

ggplot(data = data2, aes(x = variable, y = value, fill = col1)) +
  geom_bar(position = "stack", stat = "identity") +
  scale_fill_manual(values = c("#e6194B", "#ffe119", "#f58231", "#911eb4", "#42d4f4", "#bfef45")) +
  scale_y_continuous(expand = c(0, 0), labels = scales::percent) +
  facet_nested(~ col6 + ~col5, scales = "free_x", space = "free_x", switch = "x") +
  ggtitle("Title") +
  theme_classic() +
  theme(
    strip.text.y = element_text(angle = 0), legend.position = "right",
    legend.key.size = unit(0.4, "cm"),
    axis.line = element_line(colour = "black"),
    axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
    strip.placement = "outside",
    strip.background = element_rect(color = "white", fill = "white"),
    axis.title = element_blank()
  ) +
  guides(fill = guide_legend(title = NULL, ncol = 1)) +
  xlab("X axis") +
  ylab("Y axis") +
  coord_cartesian(ylim = c(.1, NA))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM