Ggplot2 - 创建一个函数,以便手动控制几个变量的线型

时间:2016-09-06 11:17:16

标签: r ggplot2 line

我想绘制几个表的值。这些表中的每一个都有不同/未知数量的变量/列。

我使用以下代码绘制数据:

library(ggplot2)
library(reshape2)
#data <- read.table("jony.csv", header = TRUE, sep = ";", fill = TRUE)
data <- read.table(text="MONTH;GFDL.ESM2M_HBV_IWW_WFDisi;GFDL.ESM2M_SWIM;GFDL.ESM2M_WaterGAP3;GFDL.ESM2M_HYPE;GFDL.ESM2M_VIC;month_mean;q70
1;853.455161290323;550.116774193548;746.965913978495;469.31688172043;546.64752688172;633.300451612903;452.931661075269
2;1037.55011792453;632.34445754717;805.189285714286;567.411202830189;763.929245283019;761.284861859839;452.931661075269
3;782.714301075269;447.378494623656;561.674193548387;422.475483870968;591.257634408602;561.100021505376;452.931661075269
", header = TRUE, sep = ";", fill = TRUE)
jony <- melt(data, id.vars="MONTH")
p <- ggplot(jony, aes(MONTH,value, col=variable))
p + geom_line(size = 0.1) +
  geom_hline(aes(yintercept = 0), linetype="dotted") +
  ylab("Runoff [m3/s]") +
  xlab("Month") +
  theme_bw() +
  theme(legend.key = element_blank())+
  scale_color_discrete(name='Models GCM_HM') +
  ggtitle("Jony")

为了手动控制变量“month_mean”和“q70”的颜色,我在代码中添加了以下功能:

f <- function(x, cols, pal = rainbow) {
  stopifnot(names(cols) %in% x)
  pal <- pal(length(x)-length(cols))
  names(pal) <- setdiff(x, names(cols))
  pal <- c(pal, cols)
  return(pal)
}
p + scale_color_manual(
  values = f(levels(jony$variable), c("month_mean"="black", "q70"="red"))
)

现在我想调整这个函数来控制两个变量“month_mean”和“q70”的行类型。我在代码中添加了以下内容:

f2 <- function(x, cols, lin = lty) {
  stopifnot(names(cols) %in% x)
  lin <- lin(length(x)-length(cols))
  names(lin) <- setdiff(x, names(cols))
  lin <- c(lin, cols)
  return(lin)
}
p + scale_linetype_manual(
  values = f2(levels(jony$variable), c("month_mean"="dotted", "q70"="dashed"))
)

但它不起作用,它甚至删除了第一个函数“f”所做的颜色效果。有谁知道为什么?

1 个答案:

答案 0 :(得分:1)

确定。正如我所说,lty是一个参数,而不是像rainbow这样的函数。因此,您不能简单地使用它生成线型。

这是一个有效的例子:

library(ggplot2)
library(reshape2)
#data <- read.table("jony.csv", header = TRUE, sep = ";", fill = TRUE)
data <- read.table(text="MONTH;GFDL.ESM2M_HBV_IWW_WFDisi;GFDL.ESM2M_SWIM;GFDL.ESM2M_WaterGAP3;GFDL.ESM2M_HYPE;GFDL.ESM2M_VIC;month_mean;q70
                   1;853.455161290323;550.116774193548;746.965913978495;469.31688172043;546.64752688172;633.300451612903;452.931661075269
                   2;1037.55011792453;632.34445754717;805.189285714286;567.411202830189;763.929245283019;761.284861859839;452.931661075269
                   3;782.714301075269;447.378494623656;561.674193548387;422.475483870968;591.257634408602;561.100021505376;452.931661075269
                   ", header = TRUE, sep = ";", fill = TRUE)
jony <- melt(data, id.vars="MONTH")
p <- ggplot(jony)

p <- p + geom_line(size = 1, aes(MONTH,value, col=variable, linetype=variable)) +
  geom_hline(aes(yintercept = 0), linetype="dotted") +
  ylab("Runoff [m3/s]") +
  xlab("Month") +
  theme_bw() +
  theme(legend.key = element_blank())+
  #scale_color_discrete(name='Models GCM_HM') +
  ggtitle("Jony")

f <- function(x, cols, pal = rainbow) {
  stopifnot(names(cols) %in% x)
  pal <- pal(length(x)-length(cols))
  names(pal) <- setdiff(x, names(cols))
  pal <- c(pal, cols)
  return(pal)
}
p + scale_color_manual(
  values = f(levels(jony$variable), c("month_mean"="black", "q70"="red"))
)

f2 <- function(x, cols) {
  stopifnot(names(cols) %in% x)
  lin <- 1:(length(x)-length(cols)) # 0:12 are the linetypes available
  names(lin) <- setdiff(x, names(cols))
  lin <- c(lin, cols)
  return(lin)
}
p + scale_linetype_manual(values = as.numeric(f2(levels(jony$variable), c("month_mean"=9, "q70"=9))))

注意:我给的变量不是month_mean或q79是线型1:5。从这个例子开始,你可以想出自己的函数来生成线型。

enter image description here