I would like to plot four lines with the legend label AND color dictated by unique values in the column, "Color_Group" and the linetype by "Line_Type_Group". Here is code needed to make the data frame, and the data frame itself.
data <- data.frame("x"=c(1,2,1,2,1,2,1,2),
"y"=1:8,
"Color_Group"=c("A","A","A","A","B","B","C","C"),
"Line_Type_Group"=c(1,1,2,2,2,2,1,1),
"Group"=c("A1","A1","A2","A2","B2","B2","C1","C1"),
stringsAsFactors=F)
> data
x y Color_Group Line_Type_Group Group
1 1 A 1 A1
2 2 A 1 A1
1 3 A 2 A2
2 4 A 2 A2
1 5 B 2 B2
2 6 B 2 B2
1 7 C 1 C1
2 8 C 1 C1
The main "Group" column is the result of pasting the color and linetype columns together, and it represents the id for each line. In order to get one legend, I initially map the color and linetype attributes to this "Group" column. Below is what I want the plot to look like.
Attempt
For simplicity's sake, I'm recreating the data frame to include columns with the literal color and linetype values.
data <- data.frame("x"=c(1,2,1,2,1,2,1,2),
"y"=1:8,"Color_Group"=c("A","A","A","A","B","B","C","C"),
"Color"=c(rep("red",4),"blue","blue","green","green"),
"Line_Type_Group"=c(1,1,2,2,2,2,1,1),
"Line_Type"=c("solid","solid","dashed","dashed","dashed","dashed","solid","solid"),
"Group"=c("A1","A1","A2","A2","B2","B2","C1","C1"),
stringsAsFactors = F)
> data
x y Color_Group Color Line_Type_Group Line_Type Group
1 1 A red 1 solid A1
2 2 A red 1 solid A1
1 3 A red 2 dashed A2
2 4 A red 2 dashed A2
1 5 B blue 2 dashed B2
2 6 B blue 2 dashed B2
1 7 C green 1 solid C1
2 8 C green 1 solid C1
line_type_group <- data$Line_Type
color_group <- data$Color
names(line_type_group) <- data$Group
names(color_group) <- data$Group
labels <- data$Color_Group
names(labels) <- data$Group
#Both attempts at setting labels variable yields plot below
#labels <- c("A","A","B","C")
g <- ggplot(data,aes(x=x,y=y,colour=Group,linetype=Group))+geom_line()
g + scale_colour_manual(name="Legend",values=color_group,labels=labels) +
scale_linetype_manual(name="Legend",values =line_type_group,labels=labels)
You could work around this by suppressing the linetype legend after changing the values
to what you want and then "manually" setting the linetypes in the colour
legend using override.aes
.
ggplot(data,aes(x=x, y=y, colour=Group, linetype=Group))+
geom_line() +
scale_colour_manual(values = color_group, labels = labels) +
scale_linetype_manual(values = line_type_group, guide = "none") +
guides(colour = guide_legend(override.aes = list(linetype = c(1, 2, 2, 1))))
this give you almost what you want
g <- ggplot(data,aes(x=x,y=y,colour=Group,linetype=Group))+geom_line()
g + scale_colour_manual(name="Legend",values=c("red", "red", "blue", "green"), labels = c("A1", "A2", "B", "C")) +scale_linetype_manual(name="Legend",values=c("solid", "dashed", "dashed", "solid"), labels = c("A1", "A2", "B", "C"))
#
g
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.