I am trying to get the code below to draw:
America - red line, Europe – green line, Asia – purple line; no markers
US – red diamond, France – green diamond, China – purple diamond
and to add a legend that has continents (colored lines with no markers) and countries (colored markers with no lines).
Any help would be appreciated.
Thanks.
#-----------------------------------------------------------------------------------------------
# packages
library(dplyr)
library(ggplot2)
# data frames
df_line <- data.frame(y=c(1,2,3), xstart=c(2,2,2), xend=c(4,4,4), label=c("America", "Asia", "Europe"), stringsAsFactors=FALSE)
df_point <- data.frame(y=c(1,2,3), x=c(1.5,1.5,1.5), label=c("US", "China", "France"), stringsAsFactors=FALSE)
# convert label to factor
df_line <- df_line %>% mutate(label=factor(label, levels=c("America", "Europe", "Asia", "US", "France", "China")))
df_point <- df_point %>% mutate(label=factor(label, levels=c("America", "Europe", "Asia", "US", "France", "China")))
## set colors
color.values <- c("red", "green", "purple", "red", "green", "purple")
# plot
p <- ggplot() +
scale_color_manual(name=NULL, values=color.values)
# segments
p <- p +
layer(
data=df_line,
mapping=aes(x=xstart, y=y, xend=xend, yend=y, colour=label),
stat="identity",
geom="segment",
params=list(linetype=1, size=1.5),
position=position_identity()
)
# points
p <- p +
layer(
data=df_point,
mapping=aes(x=x, y=y, colour=label),
stat="identity",
geom="point",
params=list(shape=18, size=6),
position=position_identity()
)