1

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"))
Kyle
  • 27
  • 5
  • Remove the alpha. – iod Dec 04 '18 at 17:39
  • You've made an area chart, which, by definition, is a filled area between some values and the x-axis. If you instead want an area bounded between 2 sets of values, that's a ribbon chart which you can make with `geom_ribbon`. It's unclear though how you want to decide which sets of values will bound that ribbon – camille Dec 04 '18 at 17:46
  • @iod Removing the alpha is a quick fix but not what I am looking for. – Kyle Dec 04 '18 at 17:55
  • @camille I'm interested in a shaded region between Vac_17 and the x-axis, H2O_17 and Vac_17, and K2SO4_17 and H2O_17. I would also like to not double shade the area between H2O_17 and Vac_17 before they cross (ideally leaving this area the same shaded color as the Vac_17 and x-axis region) When using geom_ribbon I am having a hard time giving the function the limits, can someone show me how to properly set up the geom_ribbon bound by the appropriate dates and "Type" . – Kyle Dec 04 '18 at 18:01

1 Answers1

1

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.

iod
  • 7,412
  • 2
  • 17
  • 36
  • This is looking good. I like how you changed the colors. I do use a color-blind pallet normally, I just didn't include it here so that all my theme settings could be ignored. I like how you offset the line and fill area. This might just work for my needs right now. I very much like your color choices too, I typically struggle with that and hence typically load a random preset colorblind pallett. Sounds like setting this up using geom_ribbon might be challenging. – Kyle Dec 04 '18 at 18:16
  • 1
    Glad you find this helpful. Don't forget to accept and upvote! – iod Dec 04 '18 at 19:29
  • How would I get ggplot to still display the legend? when i change the colors this way they will no longer print. – Kyle Dec 05 '18 at 14:31
  • 1
    How do you mean? They printed find on my example above. – iod Dec 05 '18 at 14:32
  • 1
    Probably has to do something with your theme/legend calls. I'll have the see the whole code to try to figure it out... – iod Dec 05 '18 at 14:33
  • I added my whole code, theme settings and all. I notice that if I run it in sections the legends disappearing seems directly related to the color calling method? – Kyle Dec 05 '18 at 15:53
  • 1
    your breaks and your labels do not correspond (the breaks all say 18 instead of 17). When I commented out the breaks segment for both scale_manual, I got the legends. – iod Dec 05 '18 at 16:23
  • 1
    As always @iod thank you so much for helping me fix my broken code, clearly I messed that up (we have several years worth of data, sometimes I get jumbled up). – Kyle Dec 05 '18 at 16:29
  • I also noticed you ended up making the fill and colour the same for each group. Is that on purpose? – iod Dec 05 '18 at 17:07
  • I was playing with the colour combinations and various symbols. I ended up sticking with the original colour combination you suggested, the offsets really make the graph look a lot better. And wow thanks for putting that much thought into it. – Kyle Dec 05 '18 at 18:49