我是R的学习者,请原谅我的错误。 我有一个数据框
QCVNTO=structure(list(O = c(1.34242268082221, 0.903089986991944, 2.55870857053317,
2.40823996531185, 1.65321251377534, 0.903089986991944, 1.20411998265592,
1.20411998265592, 1.20411998265592, 0.903089986991944, 0.903089986991944,
1.65321251377534, 1.34242268082221, 1.04139268515823, 0.903089986991944,
1.34242268082221, 1.34242268082221, 3.01029995663981, 1.34242268082221,
1.34242268082221, 1.80617997398389, 0.903089986991944, 0.903089986991944,
1.34242268082221, 1.34242268082221, 1.65321251377534, 1.20411998265592,
1.04139268515823, 1.04139268515823, 1.65321251377534, 1.34242268082221,
1.65321251377534, 0.903089986991944, 0.903089986991944, 0.903089986991944,
0.903089986991944, 1.34242268082221, 1.34242268082221, 1.04139268515823,
0.903089986991944, 0.903089986991944, 0.903089986991944, 1.95424250943932,
0.903089986991944, 0.903089986991944, 1.80617997398389, 1.34242268082221,
1.50514997831991, 1.34242268082221, 2.25767857486918, 1.80617997398389,
1.95424250943932, 2.10720996964787, 1.50514997831991, 1.50514997831991,
1.50514997831991, 1.50514997831991, 1.50514997831991, 1.95424250943932,
1.95424250943932, 1.34242268082221, 1.50514997831991, 1.50514997831991,
2.40823996531185, 1.65321251377534, 1.65321251377534, 1.50514997831991,
1.50514997831991, 1.50514997831991, 1.80617997398389, 1.50514997831991,
1.50514997831991, 1.80617997398389, 1.50514997831991, 1.50514997831991,
1.34242268082221, 1.34242268082221, 1.50514997831991, 2.55870857053317,
1.65321251377534, 1.80617997398389, 2.10720996964787, 1.80617997398389,
1.80617997398389, 1.65321251377534, 3.01029995663981, 1.65321251377534,
2.40823996531185, 1.80617997398389, 1.80617997398389, 1.65321251377534,
2.40823996531185, 1.80617997398389, 1.04139268515823, 1.65321251377534,
1.80617997398389, 2.40823996531185, 1.65321251377534, 3.01029995663981,
1.95424250943932, 1.80617997398389, 1.80617997398389, 1.50514997831991,
2.10720996964787, 1.65321251377534, 1.80617997398389, 1.50514997831991,
1.80617997398389, 2.70926996097583, 1.65321251377534, 1.95424250943932,
2.25767857486918, 2.10720996964787, 1.65321251377534, 1.80617997398389,
1.80617997398389, 1.50514997831991, 1.80617997398389, 0.903089986991944,
3.01029995663981, 2.55870857053317, 1.04139268515823, 1.80617997398389
), ProtectionStatus = c(1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0,
0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0,
0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 0, 1)), .Names = c("O", "ProtectionStatus"), row.names = c(NA,
-123L), class = "data.frame")
然后我使用代码计算了每个O'类的protectionstatus频率
df=as.data.frame(xtabs(~ ProtectionStatus + O, data = QCVNTO))
然后我绘制了堆积百分比条形图,显示了每个' O'的Protection Status
的百分比条。使用ggplot
ggplot(df,aes(x = O, y = Freq, fill = ProtectionStatus)) +
geom_bar(position = "fill",stat = "identity") +
scale_y_continuous(labels = percent, breaks = seq(0,1,by=0.1))+
labs(title = "Log 10SN50 Vs Percentage of Protection", y = "Percentage of Protection", x = "Log 10SN50")
在这一步之后我有3个问题。
1.结果图的x轴数字重叠。任何人都可以告诉我如何在x轴上将小数位数减少到2?
我需要显示栏内的百分比。
我需要在每个栏的顶部显示每个“O”类的观察/计数。 我已阅读How to center stacked percent barchart labels [希望创建[eipi10] [1]所回答的情节并尝试使用此代码
df.summary = QCVNTO %>% group_by(O) %>% +
summarise(ProtectionStatus = count(ProtectionStatus)) %>%
mutate(percent = ProtectionStatus/sum(ProtectionStatus),
pos = cumsum(percent) - 0.5*percent)
ggplot(df.summary,aes(x=QCVNTO$O,QCVNTO$ProtectionStatus,
function(x)+sum(x)),y=percent,fill=Category) +
geom_bar(stat='identity', width = .7, colour="black", lwd=0.1) +
geom_text(aes(label=ifelse(percent >= 0.07, paste0(sprintf("%.0f",
percent*100),"%"),""),y=pos), colour="white") +
coord_flip() + scale_y_continuous(labels = percent_format()) +
labs(y="", x="")
但它显示错误Aesthetics必须是长度1或与数据(2)相同:x,y。
我非常感谢大家为阅读这个问题提供宝贵的时间。
答案 0 :(得分:1)
给出
p <- ggplot(df,aes(x = O, y = Freq, fill = ProtectionStatus)) +
geom_bar(position = "fill",stat = "identity") +
scale_y_continuous(labels = scales::percent, breaks = seq(0,1,by=0.1))+
labs(title = "Log 10SN50 Vs Percentage of Protection", y = "Percentage of Protection", x = "Log 10SN50")
你可以做到
library(scales)
p + geom_text(
aes(y = Freq, label = ifelse(Freq<1&Freq>0, percent(Freq), NA)),
data=transform(df, Freq=Freq/ave(Freq, O, FUN=sum)),
position = position_stack(vjust = 0.5)) +
geom_text(aes(y=1, label = with(df, ave(Freq, O, FUN=sum))), vjust=-.5) +
scale_x_discrete(labels = function(x) round(as.numeric(x), digits=2))