我想在y轴上绘制(直方图)汽车数量,在x轴上绘制值。你能帮我解决这个问题。
我试过
ggplot(a, aes(x=value, y=cars, fill=type)) + geom_histogram()
cars type company value
car1 all company1 0.4
car2 all company1 0.6
car3 all company1 1
car1 one company1 1.2
car1 one company1 0.1
car2 one company1 0.1
car3 one company1 0.9
car1 one company1 0.44
car2 one company2 0.55
car3 one company2 0.1
car1 one company2 0
car1 one company3 0
car2 one company3 1
car3 one company3 1.2
答案 0 :(得分:1)
您需要执行以下操作才能获得所需内容:
a <- read.table(header=T , text="cars type company value
car1 all company1 0.4
car2 all company1 0.6
car3 all company1 1
car1 one company1 1.2
car1 one company1 0.1
car2 one company1 0.1
car3 one company1 0.9
car1 one company1 0.44
car2 one company2 0.55
car3 one company2 0.1
car1 one company2 0
car1 one company3 0
car2 one company3 1
car3 one company3 1.2")
library(dplyr) #you need the following line to aggregate your data per car and find mean and counts
aggregated_data <- a %>% group_by(cars) %>% summarise( mean = mean(value), counts = length(value) )
#view aggregated data
> aggregated_data
Source: local data frame [3 x 3]
cars mean counts
1 car1 0.3566667 6
2 car2 0.5625000 4
3 car3 0.8000000 4
#plot
ggplot(aggregated_data, aes(y = counts , x = mean , fill=cars ) ) + geom_bar(stat='identity')
这就是你想要的!