简体   繁体   中英

creating custom labels for multilayered plot using ggplot 2 in R

I have this plot

library(dplyr)
library(ggplot2)


indexYear <- as.numeric(2000:2010)
lDLatIndex <- rep.int(4,11) 
LDLoneYear <- c(rep.int(3,5), rep.int(2,6))  
hba1catIndex <- c(rep.int(8,6), rep.int(7.5,5))
hba1coneYear <- rep.int(7,11)

LDLeffect <- data.frame(indexYear, lDLatIndex, hba1catIndex, hba1coneYear)
LDLeffect %>% 
  ggplot(., aes(x = indexYear))+
  geom_line(aes(y = lDLatIndex, colour=rgb(237/255, 115/255,116/255)), linetype = "solid" , size = 2)+
  theme_classic()+
  theme(legend.position = "bottom")+
  ylab("mean LDL cholesterol (mmol/l)                           ")+
  xlab("Calendar year")+
  theme(axis.title = element_text(size = 17, face="bold"), axis.text = element_text(size = 17, face = "bold"))+
  scale_x_continuous(breaks = seq(2000,2015, by=1),labels = c(2000,rep("",4),2005,rep("",4), 2010, rep("",4),2015))+
  scale_y_continuous(sec.axis = sec_axis(~(.-2.15)*10.929, name = "mean HbA1c (%)                                "))+
  geom_line(aes(y = LDLoneYear, colour=rgb(237/255, 115/255,116/255)), linetype = "dashed" , size = 2)+
  geom_line(aes(y = hba1coneYear, colour=rgb(152/255, 201/255,139/255)), linetype = "twodash" , size = 2)+
  geom_line(aes(y = hba1catIndex, colour=rgb(152/255, 201/255,139/255)), linetype = "F1" , size = 2)

I know that usually, the best option is to supply data in long format for ggplot, but I couldn't get it to work. The plot above produces a strange legend that I cannot understand how got there.

I see that the names to the legend added are from the colour definitions.

What I want to make is legends that show the colour and linetype and name for each variable plotted, preferably with the option of adding custom names. I looked through a lot of pages with suggestions, but most makes use of long format which I cannot figure out because I wanted different linetypes and colours by two and two. The rest couldn't help me address this strange expression in the labelling.

Would below proposal go into right direction? Main points are: using "melt" from reshape2 for bringing data in ggplot-friendly shape. And with scale_linetype_manual and scale_colour_manual I'm explicitly providing colours and line types.

library(dplyr)
library(ggplot2)
library(reshape2) ## for "melt"

indexYear <- as.numeric(2000:2010)
lDLatIndex <- rep.int(4,11) 
LDLoneYear <- c(rep.int(3,5), rep.int(2,6))  
hba1catIndex <- c(rep.int(8,6), rep.int(7.5,5))
hba1coneYear <- rep.int(7,11)

LDLeffect <- data.frame(indexYear, lDLatIndex, hba1catIndex, hba1coneYear, LDLoneYear)

melted_df <- melt(LDLeffect, id.vars="indexYear", measure.vars=c("lDLatIndex", "hba1catIndex", "hba1coneYear", "LDLoneYear"))

ggplot(melted_df, aes(x=indexYear, value, colour=variable)) + 
  geom_line(aes(linetype=variable), size = 2) + 
  scale_linetype_manual(values=c("F1", "twodash", "solid", "dashed")) +
  scale_colour_manual(values=c(rgb(237/255, 115/255,116/255), rgb(237/255, 115/255,116/255), rgb(152/255, 201/255,139/255), rgb(152/255, 201/255,139/255))) + 
  theme_classic() + 
  theme(legend.position = "bottom")+
  ylab("mean LDL cholesterol (mmol/l)")+
  xlab("Calendar year")+
  theme(axis.title = element_text(size = 17, face="bold"), axis.text = element_text(size = 17, face = "bold"))+
  scale_x_continuous(breaks = seq(2000,2015, by=1),labels = c(2000,rep("",4),2005,rep("",4), 2010, rep("",4),2015))+
  scale_y_continuous(sec.axis = sec_axis(~(.-2.15)*10.929, name = "mean HbA1c (%)"))

在此处输入图片说明

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM