正如标题所述,我试图绘制一个堆积条形图,其中一个堆栈应该占另一个堆栈的百分比,而不是组合百分比。
应用程序正在抽样,我想将样本大小显示为总体大小的百分比。
这是我尝试过的:左侧的表格绘制到右侧的条形图中。
代码见下:
temp <- Countemf()
temp$Type <- factor(temp$Type)
temp %>%
rename(Environment=Type)%>%
tidyr::gather(Class, Size, -Environment ) %>%
ggplot(., aes(x=Environment, y=Size, fill=Class)) +
geom_bar(stat="identity",position = "fill")+
scale_y_continuous(labels = scales::percent_format())
我仍然不确定,如果我理解正确的话。但我决定用一个例子来澄清事情更容易。所以这里有一个可能的(但可能不是最优雅的)解决你的问题的方法:
temp <- dplyr::tibble(type=c("Core","Mainframe","Network","Oracle","Unix"),
sample=c(2,2,3,2,2),
pop=c(4,17,31,3,2))
temp %>%
dplyr::mutate(diff=pop-sample) %>%
tidyr::pivot_longer(cols=c(sample,pop,diff)) %>%
dplyr::filter(name!="pop") %>%
ggplot2::ggplot(ggplot2::aes(x=type,y=value,fill=name)) +
ggplot2::geom_bar(stat="identity")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句