Reputation: 223
I have the dataframe (d):
d<- structure(list(Total.VolumeD = c(1118.4, 638.4, 1180.8, 1166.4,
1296, 936, 1324.8, 936, 307.2, 1152, 691.2, 585.6, 648, 979.2,
374.4, 1358.4, 676.8, 777.6, 1344, 1286.4, 825.6, 849.6, 1056,
1785.6, 868.8, 1579.2, 283.2, 1132.8, 820.8, 1291.2, 1833.6,
614.4), Speed_KM = c(27.353, 109.412, 109.412, 78.841, 109.412,
75.623, 111.021, 114.239, 106.194, 107.803, 112.63, 109.412,
114.239, 112.63, 90.104, 45.052, 107.803, 109.412, 111.021, 111.021,
109.412, 94.931, 109.412, 64.36, 43.443, 104.585, 106.194, 106.194,
104.585, 45.052, 82.059, 102.976), Precipitation = structure(c(1L,
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L), .Label = c("0",
"0-50", "50-100", "100-200", ">200"), class = "factor")), row.names = c(NA,
-32L), class = "data.frame")
I added two geom_paths from the following two datasets:
no_d<- structure(list(q = c(1572.837685, 1310.733335, 1464.593355, 1806.375156,
1852.930416, 1844.816783, 1723.916843, 1215.776704, 1292.854462
), u = c(55L, 36L, 46L, 107L, 102L, 104L, 109L, 31L, 35L)), row.names = c(NA,
-9L), class = "data.frame")
and
with_d<- structure(list(q = c(1358.242525, 1334.898171, 1640.129166, 1648.496715,
1558.353432, 565.2914834, 1472.853017, 1303.752995, 1326.376898
), u = c(51L, 107L, 95L, 99L, 106L, 11L, 64L, 46L, 48L)), row.names = c(NA,
-9L), class = "data.frame")
I used the following code to plot what I really want. It worked really fine. However, I need to have legend for the precipitation and a another legend for the new two lines (the geom_path lines).
ggplot(d) +
geom_point(aes( x = Total.VolumeD,y = Speed_KM, width = 0.5,shape=Precipitation, color=Precipitation,
size = Precipitation))+
scale_colour_manual(values = c("lightgrey", "black", "black", "black", "black"))+
geom_path(data = no_d, aes(q, u), size = 1, linetype="solid")+
geom_path(data = with_d, aes(q, u), size = 1, linetype="dashed")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=1))+
scale_x_continuous(limits = c(0,2300))+
scale_y_continuous(limits = c(20,125))+
labs(shape = "precipitation(mm/h)")
Upvotes: 1
Views: 240
Reputation: 136
This works,
ggplot(d) +
geom_point(aes( x = Total.VolumeD,y = Speed_KM, width = 0.5,shape=Precipitation, color=Precipitation,
size = Precipitation))+
scale_colour_manual(values = c("lightgrey", "black", "black", "black", "black"))+
# add linetype in aes
geom_path(data = no_d, aes(q, u, linetype="solid"), size = 1)+
geom_path(data = with_d, aes(q, u,linetype="dashed"), size = 1)+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=1))+
scale_x_continuous(limits = c(0,2300))+
scale_y_continuous(limits = c(20,125))+
labs(shape = "precipitation(mm/h)") +
# add this
scale_linetype_discrete(name = "Y series", labels = c("solid", "dashed"))
UPDATE
ggplot(d) +
geom_point(aes(Total.VolumeD, Speed_KM, width = 0.5, shape = Precipitation,
color = Precipitation, size = Precipitation))+
scale_colour_manual(values = c("lightgrey", "black", "black", "black", "black"), guide = "none")+
geom_path(data = no_d, aes(q, u, linetype = "solid"), size = 1)+
geom_path(data = with_d, aes(q, u,linetype = "dashed"), size = 1)+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=1))+
scale_x_continuous(limits = c(0,2300))+
scale_y_continuous(limits = c(20,125))+
labs(shape = "precipitation(mm/h)") +
scale_linetype_discrete(name = "Y series", labels = c("solid", "dashed")) +
scale_size_discrete(guide = "none")
scale_colour_manual
add guide = 'none'
scale_size_discrete(guide = "none")
You are getting legends because you are giving colour
and size
in aes
.
ggplot
will return every legend parameter given inside aesthetic
.
Upvotes: 2