绘制许多变量

时间:2019-02-11 13:24:34

标签: r ggplot2

具有这样的数据框:

从像这样的数据框中:

data <- data.frame(year = c(2010,2011,2012,2010,2011,2012), 
                   name = c("stock1","stock1","stock1","stock2","stock2","stock2"), 
                   value = c(0,3,1,4,1,3))

我想创建一个情节,我用这个:

library(ggplot2)
ggplot(data=data, xName="year", groupName="name", brewerPalette="Blues")

但是我无法收到剧情。电话有问题吗?

2 个答案:

答案 0 :(得分:3)

如果您希望对条形图进行分组(如我从您的代码中猜到的那样),则此代码可能会有所帮助:

 ggplot(data = data, aes(x = as.factor(year), y = value, fill = name)) +
   geom_bar(stat = "identity", position = position_dodge(0.8), width = 0.7) +
   scale_fill_brewer(palette = "Blues")

答案 1 :(得分:1)

我认为您需要这样的东西:

library(ggplot2)
library(dplyr)
library(RColorBrewer)
df %>% 
  group_by(name) %>% 
  ggplot(aes(year,value,fill=name))+
  geom_col()+
  scale_fill_brewer(palette = "Blues")