我有這樣的情節:
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")
這會創建一個像這樣的條形圖: 請看一下
我的問題很簡單,如何將 y 軸起始值設定為 10% 而不是 0%(無需過多更改代碼)。非常感謝所有答案!(類似的問題檢查了,沒有成功...)
uj5u.com熱心網友回復:
雖然通常不建議將條形圖用于“設定”起點的一個選項是通過以下方式設定限制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))

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/493197.html
上一篇:熱圖中的重疊標簽
