下面是我的数据框
Camp <- c("Camp A", "Camp C", "Camp B","Camp C", "Camp C", "Camp B","Camp B", "Camp C", "Camp A")
Beans <- c(1,1,0,1,0,0,0,0,1)
Maize <- c(1,1,0,0,1,1,0,1,1)
oranges <- c(0,0,0,1,1,0,0,0,0)
crops <- data.frame(Camp, Beans, Maize, oranges)
我想使用上述数据集的结构来绘制一个看起来类似于此代码使用mtcars
数据集生成的barplot。
ggplot(mtcars, aes(x = reorder(row.names(mtcars), mpg), y = mpg, fill = factor(cyl))) +
geom_bar(stat = "identity") + coord_flip() + theme_minimal() + ggtitle("Fig. A: Default fill colors")
我的生成barPlot的代码如下所示。但是输出有些不同。如何更改以生成类似于mtcars
的barplot?
crops %>%
gather(-c(Camp), key = "seeds", value="count") %>%
arrange(desc(count)) %>%
ggplot(aes(x=reorder(seeds, count), y=count, fill=factor(Camp))) + geom_bar(stat = "identity") + coord_flip()