I'm attempting to create a ggplot
with two geom_line()
s and two geom_point()
s, all coming from the same dataset (which I've got as a data.table
currently). The data look like this:
> head(data)
month high_q low_q
1: 2009-03-01 0.04894481 0.1462436
2: 2009-04-01 0.04910757 0.1452902
3: 2009-06-01 0.04871459 0.1471519
4: 2009-07-01 0.04938127 0.1489580
5: 2009-08-01 0.04899900 0.1481020
6: 2009-09-01 0.04888317 0.1479808
I would like high_q
and low_q
plotted against month
, both as geom_line()
s and geom_point()
s. So far, I've got this:
Which is generated using:
require('ggplot2')
plot <- ggplot() +
geom_line(data = data,
aes(x = month,
y = high_q),
size = 0.15,
color = 'gray40') +
geom_point(data = data,
aes(x = month,
y = high_q,
color = 'HQ'),
shape = 0,
size = 1.2) +
geom_line(data = data,
aes(x = month,
y = low_q),
size = 0.15,
color = 'gray40') +
geom_point(data = data,
aes(x = month,
y = low_q,
color = 'LQ'),
shape = 2,
size = 1.2) +
scale_color_manual(values = c("skyblue4", "skyblue4")) +
theme_bw() +
theme(panel.grid.minor.x = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(color = '#CCCCCC'),
panel.grid.minor.y = element_line(color = '#CCCCCC'),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text = element_text(size = 5),
legend.text = element_text(size = 5),
legend.title = element_blank(),
legend.key.size = unit(0.3, "cm")) +
scale_y_continuous(breaks = seq(0, 1, 0.02),
limits = c(0.04, 0.16))
For my purposes, everything is turning out exactly how I'd like it to except for one small issue: the legend entries for 'HQ'
and 'LQ'
both have the triangle shape and the square shape overlapping each other (as can be seen in the plot above). I cannot figure out why this is happening, but I would like the legend for 'HQ'
and 'LQ'
to reflect the proper shapes (square and triangle respectively).
The data to reproduce this are as follows:
require('data.table') # not sure if this is necessary, but just in case
> dput(data)
structure(list(month = structure(c(15584, 15614, 15645, 15675,
14975, 15006, 15034, 15065, 15095, 15156, 15187, 15218, 15248,
15279, 15340, 15400, 15492, 15553, 15126, 15371, 15522, 15309,
15461, 15431), class = "Date"), high_q = c(0.0543422228029794,
0.0538360449888788, 0.0542576885322506, 0.0563374158664627, 0.051832022940258,
0.0511098243497093, 0.0516824037401732, 0.0508625794149931, 0.0508129898329969,
0.0522992472796989, 0.0534593224542862, 0.053658852482904, 0.0527596522451362,
0.0536379383601105, 0.0537250634604839, 0.053270060949945, 0.0551497117304649,
0.05453991405068, 0.0514309991121669, 0.0528033236299854, 0.0556533741364405,
0.0575938660077188, 0.0536383751376219, 0.0540292765156109),
low_q = c(0.150651382807563, 0.151678928161841, 0.152412405006079,
0.154346297912537, 0.148160747764117, 0.145711811622833,
0.146989273508668, 0.145807363651857, 0.146620317131406,
0.152241594600521, 0.154136431058451, 0.151915467273669,
0.151421212763034, 0.152599450134152, 0.149039642113193,
0.14629263019443, 0.149301086354786, 0.150944206416902, 0.148813843877971,
0.148000221284121, 0.152041900070419, 0.152751500401794,
0.147225953877478, 0.146177782497413)), .Names = c("month",
"high_q", "low_q"), row.names = c(NA, -24L), class = c("data.table",
"data.frame"))
It's my understanding that ggplot2
prefers data in long
format, so I always melt
my data first:
library(reshape2)
df <- melt(data, id.vars=c("month"))
You can then plot it using:
ggplot(data=df, aes(x=month, y=value, group=variable)) +
geom_line(color="gray40") +
geom_point(aes(shape=variable), colour="skyblue4") +
scale_shape_manual(values=c(0, 2)) +
theme_bw()
This gives you the the basis of what you're after, ie separate shapes in the legend. You can then add the additional themes as you wish.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.