我尝试使用两个ggplot
和两个geom_line()
创建一个geom_point()
,所有这些都来自同一个数据集(我已经将其作为一个目前data.table
。数据如下所示:
> 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
我希望high_q
和low_q
针对month
绘制,geom_line()
和geom_point()
s。到目前为止,我已经得到了这个:
使用以下方式生成:
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))
就我的目的而言,除了一个小问题外,一切都准确地说明了我的喜好:'HQ'
和'LQ'
的图例条目都有三角形和正方形形状相互重叠(如上图所示)。我无法弄清楚为什么会发生这种情况,但我希望'HQ'
和'LQ'
的图例能够反映出正确的形状(分别为正方形和三角形)。
重现这一点的数据如下:
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"))
答案 0 :(得分:1)
我的理解是ggplot2
更喜欢long
格式的数据,所以我始终melt
我的数据:
library(reshape2)
df <- melt(data, id.vars=c("month"))
然后您可以使用以下方式绘制它:
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()
这为您提供了所依据的基础,即图例中的单独形状。然后,您可以根据需要添加其他主题。