rggplot2graphingshading

Shading in ggplot


This is the data set:

structure(list(Date2 = structure(c(17702, 17703, 
17704, 17705, 
17707, 17711, 17715, 17716, 17716, 17716, 17717, 
17719, 17722, 
17728, 17736, 17738, 17738, 17738, 17739, 17741, 
17749, 17756, 
17757, 17758, 17759, 17760, 17760, 17760, 17762, 
17768, 17770, 
17771, 17772, 17773, 17774, 17774, 17774, 17775, 
17776), class = "Date"), 
Type = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 
1L, 3L, 
3L, 3L, 3L, 3L, 3L, 2L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 
2L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 1L, 3L, 3L, 
3L), .Label = c("K2SO4_17", 
"H2O_17", "Vac_17"), class = "factor", scores = 
structure(c(-36.8015441939299, 
-169.195587838832, -15.9745482460074), .Dim = 3L, 
.Dimnames = list(
c("H2O_17", "K2SO4_17", "Vac_17")))), mean_value 
= c(91.408737890633, 
4.2275822390544, 13.9666861975414, 11.7769850314945, 
9.48516623659864, 
13.1129305059853, 18.1416455043672, 20.4954137544164, 
195.444253367338, 
88.2036381498178, 7.02512304971248, 8.5167688405357, 
12.4581917402187, 
6.95313557438933, 8.90003612124658, 42.8626456690929, 
201.440665555189, 
20.0066370953826, 11.0691308980722, 13.6405283949989, 
9.78187202268978, 
10.349700146351, 9.1947286440288, 10.3610867117625, 
9.85080655497064, 
44.2794448189869, 125.343063057495, 11.1787073324726, 
10.4234407725895, 
9.66316247064878, 28.0099731759905, 13.0834417978341, 
11.4785164687325, 
5.65472508542547, 39.5686725332234, 154.554369375308, 
6.99683113513417, 
6.40467469581727, 3.88640514173422), se = 
c(56.658001167569, 
2.58885483283402, 1.93200787805785, 1.59088650666074, 
0.458746175914399, 
0.688301838822706, 2.50261347192418, 4.9669759128794, 
56.4904545919887, 
34.027450304715, 2.12027850766817, 2.37315110421232, 
5.51988739202576, 
1.92980153604852, 2.57462580378077, 13.5102180510921, 
59.523760377122, 
4.82585739331836, 2.94452462398769, 3.8952629135035, 
2.66696610575249, 
2.3294237593578, 1.80928331150276, 1.37743135944939, 
1.63654997665711, 
13.2984157319028, 32.5785380784761, 1.3900966734445, 
2.03893363992392, 
4.32316890789433, 11.1683121765524, 1.89291882660845, 
1.56533217770458, 
0.747355908089244, 6.47797452115071, 
20.1508954565148, 0.510625489349245, 
0.55146765698527, 1.64453736266876)), .Names = 
c("Date2", 
"Type", "mean_value", "se"), class = c("tbl_df", 
"tbl", "data.frame"
), row.names = c(NA, -39L))

Using this code I produce a graph with shading:

ggplot(agg_data, aes(x = Date2, y = mean_value, fill = Type)) +
  geom_area(aes(fill=Type),position = 'identity',alpha=0.4) +
  geom_line(aes(color = Type), lwd = 2) +
  geom_point(shape = 21,aes(fill=Type),lwd=3)+
  geom_errorbar(aes(ymin = mean_value - se, ymax = mean_value + se), 
                lwd = 1, width = 1)

Here is the graph that is produced. The issue here is that I want the shading to be between the lines, not between each line and the x-axis. Note that there are two different shades of blue due to the stacking of other colors. I also do not wish to get rid of the transparency, as I can't seem to make that look good.

This figure contains two different shades of blue:

Figure one: contains two different shades of blue

Plotting code with the suggested fix, a legend isn't produced.

    ggplot(agg_data, aes(x = Date2, y = mean_value, fill = Type)) +
  geom_area(aes(fill=Type),position = 'identity') +
  geom_line(aes(color = Type), lwd = 2) +
  geom_errorbar(aes(ymin = mean_value - se, ymax = mean_value + se), lwd = 
  1, width = 1) +
  geom_point(shape = 16,aes(color=Type),lwd=6)+
  guides(fill=guide_legend(title= "Legend"))+
  scale_fill_manual(name="Type",
                     breaks=c("K2SO4_18", "H2O_18", "Vac_18"),
                     labels=c("Adsorbed", "Inaccessible", "Mobile"),values=c("H2O_17"="palevioletred","K2SO4_17"="darkolivegreen3","Vac_17"="deepskyblue"))+
  scale_colour_manual(name="Type",
                     breaks=c("K2SO4_18", "H2O_18", "Vac_18"),
                     labels=c(expression(paste("K"[2],"SO"[4])), 
expression(paste("H"[2],"O")), "Pore water"),values=c("H2O_17"="palevioletred","K2SO4_17"="darkolivegreen3","Vac_17"="deepskyblue"))+
  xlab("")+
  ylab("")+
  guides(color = guide_legend(title= "Matrix" ),
        fill = guide_legend(title= "N Distribution",override.aes = 
  list(color = NA)))+
  coord_cartesian(ylim=c(0,260))+
  theme(legend.direction = 'vertical', 
    legend.key = element_rect(size=2),
    legend.key.size = unit(3, 'lines'),
    panel.grid.major = element_blank(), 
    legend.title=element_text(size=25,colour='black'),
    panel.grid.minor = element_blank(),
    legend.text=element_text(size=20),
    axis.title.y= element_text(size=40, colour='black'),
    axis.text.y = element_text(size=40, colour='black'),
    axis.ticks = element_line(colour = "black", size=2),
    axis.ticks.length = unit(0.3, "cm"),
    axis.title.x=element_blank(),
    axis.text.x=element_blank(),
    axis.ticks.x=element_blank(),
    panel.border = element_rect(colour = "black", fill=NA, size=2),
    plot.title = element_text( size=21, face="bold.italic"),
    plot.margin=unit(c(1,1,-0.5,1), "cm"))

Solution

  • The short answer is that there's no simple way to do what you're asking for given your data (see @camille's comment). However, maybe we can fix the "make it look good" part. I think one important difference between the "with alpha" and "without alpha" is that the fill colour differs from the line colour. Well - we can change that manually!

    ggplot(agg_data, aes(x = Date2, y = mean_value, 
                         fill = Type)) +
      geom_area(aes(fill=Type),position = 'identity') +
      geom_line(aes(color = Type), lwd = 2) +
      geom_point(shape = 21,aes(fill=Type),lwd=3)+
      geom_errorbar(aes(ymin = mean_value - se, ymax = 
                          mean_value + se), lwd = 1, width = 1)+
      scale_fill_manual(values=c("H2O_17"="palevioletred","K2SO4_17"="darkolivegreen3","Vac_17"="deepskyblue"))+
        scale_colour_manual(values=c("H2O_17"="palevioletred4","K2SO4_17"="darkolivegreen4","Vac_17"="deepskyblue3"))
    

    Which gives us:

    That looks better already!

    Bear in mind I'm colourblind, so the colour selection can probably be significantly improved - but you get the drift. You can also play around with line size, or maybe it's just an issue of your dev's dimensions.