R

时间:2017-10-04 03:52:15

标签: r

我有一个包含两个变量的数据框,这两个变量都是因子,教育和工资(df)。我想制作一个条形图,但只有一部分数据,教育==“高”的观察结果。

对于情节我喜欢xaxis =计数(工资)和yaxis =工资 谢谢!

起点(df):

Df1 <- data.frame(education=c("high","high","high","high","high","college","college","college","college","grad","grad","grad","grad","grad"), salary=c("65","65","65","90","65","65","65","90","90","90","90","65","75","75"))
到目前为止

代码:

library(dplyr)
library(ggplot2)
Df2 <- Df1 %>%
filter(education == "high") %>%
mutate(SCount = sum(n))
ggplot(Df2, aes(x =salary, y=SCount) + geom_bar(stat = "identity") +
coord_flip())

2 个答案:

答案 0 :(得分:0)

你的代码中有一些拼写错误...但如果我正确地解释你想要完成的事情,那么这就是你想要的:

library(dplyr)
library(ggplot2)

Df1 <- data.frame(
  education = c("high", "high", "high", "high", "high", "college", "college", "college", "college", "grad", "grad", "grad", "grad", "grad"),
  salary = c("65", "65", "65", "90", "65", "65", "65", "90", "90", "90", "90", "65", "75", "75")
)

Df2 <- Df1 %>%
  filter(education == "high") %>%
  group_by(education, salary) %>%
  summarise(SCount = n())

ggplot(Df2, aes(x = salary, y = SCount)) +
  geom_bar(stat = "identity") +
  coord_flip()

..产生这个情节

enter image description here

答案 1 :(得分:0)

Esta esmisolución

Df1 <- data.frame(education= c(rep("high",5),rep("college",4),rep("grad",5)),
       salary=rep(c(65,90,65,90,65,75),c(3,1,3,4,1,2)))
library(sjPlot)
sjp.frq(subset(Df1,education == "high")$salary,show.prc= F,show.n = F,coord.flip = T)