noriega
noriega

Reputation: 448

R + ggplot: plotting many variables

I want to make a plot using this data frame:

val = c(27.9, 35.5, 28.2, 20.7, 9.3, 7.3, 9.2, 8.8, 14.2, 13.7, 12.4, 41.3, 31.8,  
                44.0, 28.0, 17.5, 6.1, 6.9, 5.7,  9.8,  8.8, 21.9, 13.7, 26.2, 22.8,    
                21.6, 15.2, 15.2,  3.4, 2.9,  4.2,  3.3,  8.1,  9.2,  8.5, 25.8, 34.1,  
               36.6, 28.1, 10.4, 3.9, 15.8, 24.9, 19.5, 21.8, 22.2, 19.1, 22.0)
df = data.frame(variable=rep(c('E1', 'E2'), 
                each=12,2),
                value=val, 
                mes= rep(month.abb,4),
                var=rep(c('orig', 'trat'), each=24))
str(df)
    'data.frame':   48 obs. of  4 variables:
     $ variable: Factor w/ 2 levels "E1","E2": 1 1 1 1 1 1 1 1 1 1 ...
     $ value   : num  27.9 35.5 28.2 20.7 9.3 7.3 9.2 8.8 14.2 13.7 ...
     $ mes     : Factor w/ 12 levels "Apr","Aug","Dec",..: 5 4 8 1 9 7 6 2 12 11 ...
     $ var     : Factor w/ 2 levels "orig","trat": 1 1 1 1 1 1 1 1 1 

Exploring, I made this plot:

ggplot(df, 
       aes(mes, value, group=variable, color=variable, shape=var)) + 
        scale_x_discrete(limits = month.abb) + 
        geom_point() + geom_line() + 
        theme(legend.position = 'bottom')

The ggplot output is not what I expected but when passed the ggplotly function it becomes what I intended.

Besides, if I pass the option: ...aes(mes, value, group=variable, color=variable, shape=var, linetype=var)... in order to separate both var values using different line types I get the error: Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line.

So, what happens with the ggplot graphic? and how should I use the linetype attribute inside the ggplot function?

Edit. fausto.siegmund's answer allowed me to separate the var variables using different type of lines:

ggplot(df, 
       aes(mes, value, group=interaction(variable,var), color=variable, linetype=var)) + 
  scale_x_discrete(limits = month.abb) + 
  geom_point() + geom_line() + 
  theme(legend.position = 'bottom')

Now it looks better. It's possible to appreciate both var factors at a glimpse.

Thank you fausto.siegmund!

Upvotes: 0

Views: 1671

Answers (1)

gregor-fausto
gregor-fausto

Reputation: 660

To see the combination of factor levels in var and variable that you need to plot, run levels(interaction(df$variable,df$var)). This shows that the factors to plot are:

"E1.orig" "E2.orig" "E1.trat" "E2.trat"

To plot unique lines for these possible combinations, modify group=variable to group=interaction(variable,var).

Full code and resulting plot.

val = c(27.9, 35.5, 28.2, 20.7, 9.3, 7.3, 9.2, 8.8, 14.2, 13.7, 12.4, 41.3, 31.8,  
                44.0, 28.0, 17.5, 6.1, 6.9, 5.7,  9.8,  8.8, 21.9, 13.7, 26.2, 22.8,    
                21.6, 15.2, 15.2,  3.4, 2.9,  4.2,  3.3,  8.1,  9.2,  8.5, 25.8, 34.1,  
               36.6, 28.1, 10.4, 3.9, 15.8, 24.9, 19.5, 21.8, 22.2, 19.1, 22.0)
df = data.frame(variable=rep(c('E1', 'E2'), 
                each=12,2),
                value=val, 
                mes= rep(month.abb,4),
                var=rep(c('orig', 'trat'), each=24))

ggplot(df, 
       aes(mes, value, group=interaction(variable,var), color=variable, shape=var)) + 
  scale_x_discrete(limits = month.abb) + 
  geom_point() + geom_line() + 
  theme(legend.position = 'bottom')

enter image description here

Upvotes: 1

Related Questions