Help with legends

32 views
Skip to first unread message

Casault, Benoit

unread,
Nov 21, 2016, 3:42:21 PM11/21/16
to ggp...@googlegroups.com

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()

                )

 

 

 

 

Crump, Ron

unread,
Nov 23, 2016, 8:39:54 AM11/23/16
to Casault, Benoit, ggp...@googlegroups.com
Hi Benoit,

>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).

I think the code below meets your requirements without making too many
tweaks to your code.

I've never used layer rather than a geom_* function is this the preferred
style now? Or just an alternative one?

Ron.

#--------------------------------------------------------------------------
-
# packages
library(dplyr)
library(ggplot2)

# data frames
DF <- data.frame( y=c(1,2,3,1,2,3), xstart=c(2,2,2,NA,NA,NA),
xend=c(4,4,4,NA,NA,NA), x=c(NA,NA,NA,1.5,1.5,1.5),
label=c("America", "Asia", "Europe",
"US", "China", "France"),
stringsAsFactors=FALSE )

# convert label to factor
DF <- DF %>% mutate(label=factor(label, levels=c("America", "Europe",
"Asia", "US", "France", "China")))

## set marker values, then we will use label to set the colour,
## linetype and shape aesthetics
color.values <- c("red", "green", "purple", "red", "green", "purple")
line.values <- c(1,1,1,0,0,0)
shape.values <- c(NA,NA,NA,18,18,18)

# plot
p <- ggplot(DF) +
scale_color_manual(name=NULL, values=color.values) +
scale_linetype_manual(name=NULL, values=line.values) +
scale_shape_manual(name=NULL, values=shape.values)

# segments
p <- p +
layer(
mapping=aes(x=xstart, y=y, xend=xend, yend=y, colour=label,
linetype=label),
stat="identity",
geom="segment",
params=list(size=1.5),
position=position_identity()
)

# points
p <- p +
layer(
mapping=aes(x=x, y=y, colour=label, shape=label),
stat="identity",
geom="point",
params=list(size=6),
position=position_identity()
)


Reply all
Reply to author
Forward
0 new messages