在R中计算百分比并制作ggplot条形图

时间:2019-08-23 16:04:52

标签: r ggplot2

我只是在学习R,并尝试通过Excel复制可以在PivotTable中轻松创建的内容。我在下面的数据中列出了状态名称及其状态。我想制作一个水平条形图,在Y轴上显示状态名称,在X轴上显示以下百分比。

state_name status
State 1 above
State 1 above
State 1 below
State 1 below
State 1 below
State 1 above
State 1 below
State 1 below
State 1 below
State 1 above
State 2 above
State 2 NA
State 2 NA
State 2 NA
State 2 NA
State 3 below
State 3 above
State 3 above
State 3 above
State 3 below
State 3 above
State 3 below
State 3 below
State 3 above

我可以加载数据,但是不确定如何将代码编写到子集并创建百分比。

这是我可怜的尝试,

ggplot(data = subset(data, !is.na(status)), aes(y=state_name, x=count(status[below])/count(status))) +
  geom_bar(stat="identity")

任何帮助将不胜感激。我通过例子学习得最好。

2 个答案:

答案 0 :(得分:2)

您可以使用prop.table来获得percantages,

data_perc <- as.data.frame(prop.table(table(data), 1))
data_perc <- data_perc[data_perc$status=="below",]


ggplot(data= data_perc, aes(x=state_name,y= Freq ,fill=state_name)) +
  geom_bar(stat="identity") + 
  coord_flip() +
  ggtitle("My Bar Chart")

给予

enter image description here

数据:

data <- read.table(text="state_name status
State1 above
State1 above
State1 below
State1 below
State1 below
State1 above
State1 below
State1 below
State1 below
State1 above
State2 above
State2 NA
State2 NA
State2 NA
State2 NA
State3 below
State3 above
State3 above
State3 above
State3 below
State3 above
State3 below
State3 below
State3 above",header=T)

答案 1 :(得分:1)

我将您的数据另存为state_1等,并加载它:

states <- read.table("c:/R_files/SO.dat", header = TRUE)
library(ggplot2)
library(dplyr)
ggplot(states, aes(state_name, status)) + geom_col() + coord_flip()

states %>% 
  group_by(state_name) %>% 
  summarise(pct = 100 * length(which(status=="below"))/length(status)) %>% 
ggplot(aes(x = state_name,
           y = pct)) +  geom_col(fill = "blue") + coord_flip()

enter image description here