Elr Mant
Elr Mant

Reputation: 527

Plot many variables

Having a dataframe like this one:

From a dataframe like this one:

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))

I would like to create a plot and I use this:

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

but I can't receive the plot. Anything wrong in the call?

Upvotes: 0

Views: 83

Answers (2)

maaniB
maaniB

Reputation: 605

If you want a grouped bar plot (as I guessed from your code), this code may be helpful:

 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")

Upvotes: 1

NelsonGon
NelsonGon

Reputation: 13309

I think you need something like this:

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

Upvotes: 1

Related Questions