Panos Kalatzantonakis
Panos Kalatzantonakis

Reputation: 12683

Seperate styles in grouped ggplot

I would like to create a different line-style in a ggplot with a group, and different colors in the confident interval area.

My dataframe consists of the following data:

structure(list(optz = c(26362, 26362, 26362, 26362, 26362, 26362, 
26362, 26362, 26362, 26362, 26362, 26362, 26362, 26362, 26362, 
26362, 26362, 26362, 26362, 26362, 26362, 26362, 26362, 26362, 
26362, 26362, 26362, 26362, 26362, 26362, 26362, 26362, 26362, 
26362, 26362, 26362, 26362, 26362, 26362, 26362, 26362, 26362, 
26362, 26362, 26362, 26362, 26362, 26362, 26362, 26362, 26362, 
26362, 26362, 26362, 26362, 26362, 26362, 26362, 26362, 26362
), error = c(2.8809, 2.956, 3.1093, 2.1273, 2.619, 3.2374, 3.0701, 
2.7519, 2.6478, 2.1963, 2.4243, 2.7949, 2.2362, 2.1745, 2.8845, 
2.9595, 2.1128, 2.4568, 2.1164, 2.6478, 2.716, 2.2471, 2.6981, 
2.2543, 2.2688, 2.3774, 2.7878, 2.3701, 1.8905, 1.843, 2.0982, 
2.2652, 2.2579, 2.5398, 2.3521, 1.9015, 2.4243, 2.5542, 2.6657, 
2.6514, 2.0546, 2.7698, 2.4207, 2.9524, 2.6909, 2.7411, 2.3991, 
2.1636, 2.2833, 2.7304, 2.0909, 1.8394, 2.1418, 1.6856, 2.334, 
2.3014, 1.649, 2.2072, 2.2253, 2.2398), step = c(10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 30L, 30L, 
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 
30L, 30L, 30L, 30L, 30L), method = structure(c(2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("QV", "VN"), class = "factor")), row.names = 2:61, class = "data.frame")

I use the following code to plot:

  ggplot(data = df, aes(x = step, y = error, group = method, color = method)) +
    stat_smooth(data = df, level = 0.95) +
    theme(panel.background = element_rect(fill = "#EBEBEB",colour = "#CACACA", linetype = "solid"))+
    ggtitle('M1 vs M2')+
    ylab('error')+
    xlab('step') 

enter image description here

When I add this line:

geom_line(aes(color = method, linetype = 4, alpha = 0.8), size = 2) 

to change the line style, I get this error:

Error: A continuous variable can not be mapped to linetype

Upvotes: 1

Views: 50

Answers (1)

Duck
Duck

Reputation: 39613

Is this what you are looking for?

#Code
ggplot(data = df, aes(x = step, y = error,
                      group = method,
                      color = method,
                      linetype = method,
                      alpha=method)) +
  stat_smooth(data = df, level = 0.95) +
  theme(panel.background = element_rect(fill = "#EBEBEB",colour = "#CACACA", linetype = "solid"))+
  ggtitle('M1 vs M2')+
  ylab('error')+
  xlab('step')+
  scale_linetype_manual(values=c(1,4))+
  scale_alpha_manual(values=c(1,0.8))

Output:

enter image description here

Or maybe this:

#Code 2
ggplot(data = df, aes(x = step, y = error,
                      group = method,
                      color = method,
                      linetype = method,
                      alpha=method,
                      fill=method)) +
  stat_smooth(data = df, level = 0.95) +
  theme(panel.background = element_rect(fill = "#EBEBEB",colour = "#CACACA", linetype = "solid"))+
  ggtitle('M1 vs M2')+
  ylab('error')+
  xlab('step')+
  scale_linetype_manual(values=c(1,4))+
  scale_alpha_manual(values=c(1,0.8))+
  scale_fill_manual(values=c('blue','pink'))+
  scale_color_manual(values=c('red','purple'))

Output:

enter image description here

Upvotes: 2

Related Questions