Allen
Allen

Reputation: 41

Simple ggplot2 situation with colors and legend

Trying to make some plots with ggplot2 and cannot figure out how colour works as defined in aes. Struggling with errors of aesthetic length.

I've tried defining colours in either main ggplot call aes to give legend, but also in geom_line aes.

# Define dataset:
number<-rnorm(8,mean=10,sd=3)
species<-rep(c("rose","daisy","sunflower","iris"),2)
year<-c("1995","1995","1995","1995","1996","1996","1996","1996")

d.flowers<-cbind(number,species,year)
d.flowers<-as.data.frame(d.flowers)

#Plot with no colours:
ggplot(data=d.flowers,aes(x=year,y=number))+
  geom_line(group=species)             # Works fine

#Adding colour:
#Defining aes in main ggplot call:
ggplot(data=d.flowers,aes(x=year,y=number,colour=factor(species)))+
  geom_line(group=species)      
# Doesn't work with data size 8, asks for  data of size 4

ggplot(data=d.flowers,aes(x=year,y=number,colour=unique(species)))+
  geom_line(group=species)         
# doesn't work with data size 4, now asking for data size 8

The first plot gives Error: Aesthetics must be either length 1 or the same as the data (4): group

The second gives Error: Aesthetics must be either length 1 or the same as the data (8): x, y, colour

So I'm confused - when given aes of length either 4 or 8 it's not happy!

How could I think about this more clearly?

Upvotes: 0

Views: 184

Answers (1)

dbo
dbo

Reputation: 1234

Here are @kath's comments as a solution. It's subtle to learn at first but what goes inside or outside the aes() is key. Some more info here - When does the aesthetic go inside or outside aes()? and lots of good googleable "ggplot aesthetic" centric pages with lots of examples to cut and paste and try.

library(ggplot2)
number <- rnorm(8,mean=10,sd=3)
species <- rep(c("rose","daisy","sunflower","iris"),2)
year <- c("1995","1995","1995","1995","1996","1996","1996","1996")
d.flowers <- data.frame(number,species,year, param1, param2)
head(d.flowers)

 #number   species year 
 #1 8.957372      rose 1995     
 #2 7.145144     daisy 1995     
 #3 9.864917 sunflower 1995      
 #4 7.645287      iris 1995     
 #5 4.996174      rose 1996      
 #6 8.859320     daisy 1996     

 ggplot(data = d.flowers, aes(x = year,y = number,
                          group = species, 
                         colour = species)) + geom_line()  

enter image description here

 #note geom_point() doesn't need to be grouped - try:
  ggplot(data = d.flowers, aes(x = year,y = number, colour = species)) + geom_point() 

Upvotes: 1

Related Questions