简体   繁体   English

减少 r 中 ggplot2 中 x 轴值的小数位数

[英]reducing number of decimal places in x axis values in ggplot2 in r

I am learner of R so excuse me for mistakes.我是 R 的学习者,所以请原谅我的错误。 I am having a dataframe我有一个数据框

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")

Then i have calculated the freq of protectionstatus for each 'O'class using the code然后我使用代码计算了每个“O”类的保护状态频率

df=as.data.frame(xtabs(~ ProtectionStatus + O, data = QCVNTO))

Then I have plotted stacked percentage bar plot showing percentage bar of Protection Status of each 'O' class using ggplot然后我绘制了堆叠百分比条形图,使用ggplot显示每个“O”类的Protection Status百分比条

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")

I have 3 questions after this step.在这一步之后我有 3 个问题。

1.The resulting plot is having x-axis digits overlapping. 1.生成的图具有 x 轴数字重叠。 can any one show me how to reduce the number of decimals to 2 in x axis?谁能告诉我如何将x轴的小数位数减少到2?

  1. I need to show the percentages inside the bar.我需要在栏中显示百分比。

  2. I need to show in top of each bar the number of oberservations/count for each 'O'class.我需要在每个条形图的顶部显示每个“O”类的观察次数/计数。 I have read How to center stacked percent barchart labels [want to create plot as answered by [eipi10][1] and tried with this code我已阅读如何将堆积百分比条形图标签居中[想要创建由 [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="")

but it shows the error Aesthetics must be either length 1 or the same as the data (2): x, y.但它显示错误美学必须是长度 1 或与数据 (2): x, y 相同。

I really thank you all for providing your valuable time in reading this question.我真的很感谢你们提供宝贵的时间来阅读这个问题。

Given给定的

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")

you could do你可以

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)) 

在此处输入图片说明

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

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