简体   繁体   English

ggplot2 - 因子颜色和图例调整

[英]ggplot2 - factor colour and legend adjustment

I have written a short script to plot trends of measured radioactivity activities on 2 separate measurement devices. 我写了一个简短的脚本,用于在两个独立的测量设备上绘制测量放射性活动的趋势。 The script is shown below 该脚本如下所示

pkgLoad <- function(x)
  {
    if (!require(x,character.only = TRUE))
    {
      install.packages(x,dep=TRUE, repos='http://star-www.st-andrews.ac.uk/cran/')
      if(!require(x,character.only = TRUE)) stop("Package not found")
    }

  }

pkgLoad("ggplot2")
pkgLoad("XLConnect")
pkgLoad("reshape2")

#Load the workbook
wb<-loadWorkbook("CapintecQC.xlsx")

df_blue <-readWorksheet(wb, sheet = "Blue", startCol=1, endCol=6)
#sort date format
df_blue$Date <- as.Date(df_blue$Date , "%d/%m/%y")
df_blue[order(df_blue$Date ),]

df_gold <-readWorksheet(wb, sheet = "Gold", startCol=1, endCol=6)
df_gold$Date <- as.Date(df_gold$Date , "%d/%m/%y")
df_gold[order(df_gold$Date ),]

#Reference Cs-137 details
ref_activity <- 9.3
half_life <- 30.23
ref_date <- as.Date('06/01/08',format='%d/%m/%y')

blue_melt <- melt(df_blue[,c(1,2:6)], id="Date", value.name="Activity", variable.name="Isotope")

#Add new column to data frame with expected activity
df_gold["Exp_Act"] <- round(ref_activity*exp((-0.693/half_life)*as.numeric(difftime(df_gold$Date,ref_date))/365.25),3)
df_gold["Exp_Act_0.95"] <- 0.95 * df_gold$Exp_Act
df_gold["Exp_Act_1.05"] <- 1.05 * df_gold$Exp_Act 


gold_melt <- melt(df_gold[,c(1,2:6)], id="Date", value.name="Activity", variable.name="Isotope")

p <- ggplot( NULL )+geom_point(data = gold_melt, aes(x=Date,y=Activity, col=Isotope)) + geom_ribbon(data = df_gold, aes(x = Date, ymin = Exp_Act_0.95, ymax = Exp_Act_1.05), fill='blue', alpha=0.2) + geom_point(data = blue_melt, aes(x=Date,y=Activity, col=Isotope), shape=2) + theme_bw()
print(p)

在此输入图像描述

I am not very competent with R/ggplot2. 我对R / ggplot2不太称职。 I would like the final plot to show the measured activity for each radionuclide to be the same color for both devices (ie Cs-137 in red, 99mTc in Blue). 我想最后的图表显示每种放射性核素的测量活动对于两种器件都是相同的颜色(即红色的Cs-137,蓝色的99mTc)。 How can I do this as my graph plots different colours. 我怎么能这样做,因为我的图表绘制了不同的颜色。

Also the legend is non-pleasing. 传奇也不令人满意。 (i) The format for each nuclide, which is picked up from the excel header changes from Cs-137 to Cs.137. (i)从excel标题中拾取的每个核素的格式从Cs-137变为Cs.137。 How can I have Cs-137, Tc-99m etc as headers? 如何将Cs-137,Tc-99m等作为标题? (ii) Each radionuclide is duplicated in the legend - one for each device. (ii)每个放射性核素都在图例中重复 - 每个设备一个。 Is it possible to show just the legend for the first data frame (df_gold) or better just have text in the legend, with the text color matched to the marker color in the plot?) 是否可以只显示第一个数据框(df_gold)的图例,或者更好的是只有图例中的文字,文字颜色与图中的标记颜色相匹配?)

df_gold structure df_gold结构

structure(list(Date = structure(c(15708, 15709, 15712, 15713, 
15714, 15715, 15716, 15719, 15720, 15721, 15722, 15723, 15726, 
15727, 15729, 15730, 15733, 15734, 15735, 15736, 15740, 15741, 
15743, 15747, 15748, 15749, 15750, 15751, 15754, 15755, 15756, 
15757, 15758, 15761, 15762, 15764, 15765, 15768, 15769, 15770, 
15771, 15772, 15775, 15776, 15777, 15779, 15782, 15783, 15784, 
15785, 15786, 15789, 15790, 15791, 15792, 15797, 15798, 15799, 
15800), class = "Date"), Cs..137 = c(8.2, 8.1, 8.1, 8.1, 8.1, 
8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 
8.1, 8.1, 8.1, 8.2, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8, 8.2, 
8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 
8.1, 8.1, 8.1, 8, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 8.1, 
8.1, 8.1), In..111 = c(6.49, 6.47, 6.48, 6.43, 6.49, 6.51, 6.5, 
6.47, 6.48, 6.4, 6.48, 6.48, 6.48, 6.49, 6.49, 6.47, 6.48, 6.48, 
6.5, 6.47, 6.49, 6.55, 6.46, 6.49, 6.48, 6.48, 6.46, 6.48, 6.49, 
6.44, 6.49, 6.46, 6.45, 6.46, 6.46, 6.43, 6.49, 6.47, 6.45, 6.43, 
6.44, 6.44, 6.44, 6.46, 6.45, 6.47, 6.45, 6.43, 6.44, 6.47, 6.45, 
6.46, 6.45, 6.46, 6.39, 6.46, 6.44, 6.42, 6.41), I..123 = c(6.97, 
6.94, 6.96, 6.91, 6.92, 6.95, 6.93, 6.92, 6.93, 7, 6.97, 6.96, 
6.96, 6.94, 6.98, 6.97, 6.95, 6.95, 6.94, 6.96, 6.97, 7.01, 6.92, 
7, 6.98, 6.97, 6.91, 6.99, 6.95, 6.88, 6.96, 6.91, 6.91, 6.93, 
6.94, 6.94, 6.97, 6.93, 6.93, 6.93, 6.96, 6.94, 6.94, 6.92, 6.93, 
6.91, 6.93, 6.92, 6.92, 6.91, 6.91, 6.89, 6.92, 6.9, 6.9, 6.91, 
6.91, 6.9, 6.9), I..131 = c(10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 
10.5, 10.5, 10.5, 10.8, 10.5, 10.6, 10.5, 10.5, 10.5, 10.5, 10.5, 
10.5, 10.5, 10.5, 10.5, 10.6, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 
10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.4, 
10.5, 10.4, 10.5, 10.5, 10.5, 10.4, 10.5, 10.4, 10.4, 10.5, 10.4, 
10.4, 10.4, 10.4, 10.4, 10.3, 10.5, 10.5, 10.5, 10.6), Tc..99m = c(15, 
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15.1, 15, 15, 15.1, 15, 
15, 15, 15, 15.1, 15, 15.1, 15, 15, 15, 15, 15, 15, 15, 15, 15, 
15, 15, 15, 15, 14.9, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 
14.9, 14.8, 14.9, 14.9, 14.9, 14.9, 15, 15, 14.8, 15, 15, 15, 
15), Exp_Act = c(8.294, 8.293, 8.292, 8.291, 8.291, 8.29, 8.29, 
8.288, 8.288, 8.287, 8.287, 8.286, 8.285, 8.284, 8.283, 8.283, 
8.281, 8.28, 8.28, 8.279, 8.277, 8.277, 8.276, 8.274, 8.273, 
8.273, 8.272, 8.272, 8.27, 8.27, 8.269, 8.269, 8.268, 8.266, 
8.266, 8.265, 8.264, 8.263, 8.262, 8.262, 8.261, 8.261, 8.259, 
8.259, 8.258, 8.257, 8.256, 8.255, 8.255, 8.254, 8.254, 8.252, 
8.251, 8.251, 8.25, 8.248, 8.247, 8.247, 8.246), Exp_Act_0.95 = c(7.8793, 
7.87835, 7.8774, 7.87645, 7.87645, 7.8755, 7.8755, 7.8736, 7.8736, 
7.87265, 7.87265, 7.8717, 7.87075, 7.8698, 7.86885, 7.86885, 
7.86695, 7.866, 7.866, 7.86505, 7.86315, 7.86315, 7.8622, 7.8603, 
7.85935, 7.85935, 7.8584, 7.8584, 7.8565, 7.8565, 7.85555, 7.85555, 
7.8546, 7.8527, 7.8527, 7.85175, 7.8508, 7.84985, 7.8489, 7.8489, 
7.84795, 7.84795, 7.84605, 7.84605, 7.8451, 7.84415, 7.8432, 
7.84225, 7.84225, 7.8413, 7.8413, 7.8394, 7.83845, 7.83845, 7.8375, 
7.8356, 7.83465, 7.83465, 7.8337), Exp_Act_1.05 = c(8.7087, 8.70765, 
8.7066, 8.70555, 8.70555, 8.7045, 8.7045, 8.7024, 8.7024, 8.70135, 
8.70135, 8.7003, 8.69925, 8.6982, 8.69715, 8.69715, 8.69505, 
8.694, 8.694, 8.69295, 8.69085, 8.69085, 8.6898, 8.6877, 8.68665, 
8.68665, 8.6856, 8.6856, 8.6835, 8.6835, 8.68245, 8.68245, 8.6814, 
8.6793, 8.6793, 8.67825, 8.6772, 8.67615, 8.6751, 8.6751, 8.67405, 
8.67405, 8.67195, 8.67195, 8.6709, 8.66985, 8.6688, 8.66775, 
8.66775, 8.6667, 8.6667, 8.6646, 8.66355, 8.66355, 8.6625, 8.6604, 
8.65935, 8.65935, 8.6583)), row.names = c(NA, -59L), .Names = c("Date", 
"Cs..137", "In..111", "I..123", "I..131", "Tc..99m", "Exp_Act", 
"Exp_Act_0.95", "Exp_Act_1.05"), class = "data.frame")

df_blue structure df_blue结构

structure(list(Date = structure(c(15790, 15791, 15792, 15797, 
15798, 15799, 15800), class = "Date"), Cs.137 = c(8.1, 8.2, 8.2, 
8.2, 8.2, 8.2, 8.2), I.123 = c(6.82, 6.85, 6.91, 6.84, 6.82, 
6.82, 6.83), I.131 = c(10.5, 10.6, 10.6, 10.5, 10.6, 10.6, 10.6
), In.111 = c(6.35, 6.45, 6.43, 6.37, 6.38, 6.4, 6.37), X99m.Tc = c(15, 
15, 15.1, 15.1, 15.1, 15.1, 15.1)), .Names = c("Date", "Cs.137", 
"I.123", "I.131", "In.111", "X99m.Tc"), row.names = c(NA, -7L
), class = "data.frame")

My approach would be to bind together both data frames and then add new column that contains name of device (gold or blue). 我的方法是将两个数据框绑定在一起,然后添加包含设备名称(金色或蓝色)的新列。

df<-rbind(gold_melt,blue_melt)
df$device<-rep(c("gold","blue"),c(nrow(gold_melt),nrow(blue_melt)))

With function recode() from library car change names of Isotope as they should be. 使用函数recode()从库car改变Isotope名称,因为它们应该是。

df$Isotope<-recode(df$Isotope,"c('Cs..137','Cs.137')='Cs-137';
       c('I..123','I.123')='I-123';
       c('I..131','I.131')='I-131';
       c('In..111','In.111')='In-111'
       ;c('Tc..99m','X99m.Tc')='Tc-99m'")

Now you just need one call to geom_point() using new data frame. 现在您只需要使用新数据框调用geom_point() I added also shape=device to get different shapes for each device. 我还添加了shape=device来为每个设备获得不同的形状。

ggplot(NULL) + 
  geom_point(data=df,aes(x=Date,y=Activity, col=Isotope,shape=device))+
  geom_ribbon(data = df_gold, aes(x = Date, ymin = Exp_Act_0.95, ymax = Exp_Act_1.05), fill='blue', alpha=0.2)

在此输入图像描述

Just in case you'd want to "fuse" the legend together, then building up on Didzis' answer: 万一你想把这个传说融合在一起,然后建立在Didzis的答案上:

df <- transform(df, device = factor(device, levels=unique(device)), 
                          grp = paste(Isotope, device, sep="_"))

require(RColorBrewer)
ggplot() + geom_point(data = df, aes(x = Date, y = Activity, 
                       colour=grp, shape = grp, fill=grp)) + 
geom_ribbon(data = df_gold, aes(x = Date, ymin = Exp_Act_0.95, 
                    ymax = Exp_Act_1.05), fill='blue', alpha=0.2) + 
scale_shape_manual("", values=rep(c(21,24), 5)) + 
scale_fill_manual("", values=rep(brewer.pal(5, "Set1"), each=2)) + 
scale_colour_manual("", values=rep(brewer.pal(5, "Set1"), each=2))

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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