在 mycol 参数中使用 ifelse 正确分配颜色以突出显示 xyplot 意大利面图中的线的令人沮丧的问题
Frustrating issue with CORRECTLY assigning color, using ifelse in mycol argument, to highlight line in xyplot spaghetti plot
我正在尝试为我的意大利面条图中的一条线涂上明亮的颜色,以将其与其他线(其他线为灰色)区分开来。在颜色参数中使用一个简单的 ifelse 语句(或创建一个单独的变量 - 都尝试过)应该可以解决问题,但这与正确的国家/地区标签不一致。以国家/地区标签为条件 可以在绘图或标记时正确识别感兴趣的特定国家/地区。但是,在带有 col = 参数的 ifelse 语句中使用它时,突出显示的行会与不正确的国家/地区标签混淆。我想知道以前是否有人遇到过这个问题。非常感谢任何见解。 Reprex 包含在下面。
Data <- data.frame(
score = round(runif(25,0,1), 2),
label = c("ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL"),
year = c(2000,2000,2000,2000,2000,2001, 2001, 2001,2001,2001, 2002, 2002,2002,2002,2002,2003,2003,2003,2003,2003,2004,2004,2004,2004,2004))
library(lattice)
library(directlabels)
## just plotting ZAF label - this is what it should look like
xyplot(Data$score[Data$label == "ZAF"] ~ Data$year[Data$label == "ZAF"], type = "l", xlab = "Year", ylab = "Democracy Score", col = ifelse(Data$label == "ZAF", "red", "grey"))
## now plotting all labels - highlights the wrong label in red, should still highlight ZAF
myplot <- xyplot(Data$score ~ Data$year, group = Data$label, type = "l", xlab = "Year", ylab = "Democracy Score", col = ifelse(Data$label == "ZAF", "red", "grey"))
direct.label(myplot)```
我查看了 here,发现您可以设置 par.settings
。但是您必须按顺序执行此操作,并且标签是按字母顺序排列的。因此,由于 CHL 在您的标签中排在第一位,因此它的颜色需要在 col-vector 中排在第一位。请根据您的喜好更改颜色。
set.seed(123)
df <- data.frame(
score = round(runif(25,0,1), 2),
label = c("ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL"),
year = c(2000,2000,2000,2000,2000,2001, 2001, 2001,2001,2001, 2002, 2002,2002,2002,2002,2003,2003,2003,2003,2003,2004,2004,2004,2004,2004))
library(lattice)
library(directlabels)
myplot <- xyplot(score ~ year,
data = df,
group = label,
auto.key = T,
type = "l",
xlab = "Year", ylab = "Democracy Score",
par.settings = list(
superpose.symbol = list(col = c("red","blue","blue","blue","blue"),pch = 19),
superpose.line = list(col = c("red","blue","blue","blue","blue"),lwd = 2))
)
print(direct.label(myplot))
ggplot中的加法
这里是如何在 ggplot
中完成此操作的示例。这只是实现此目的的众多方法之一。另一种方法是使用包 gghighlight
.
set.seed(123)
df <- data.frame(
score = round(runif(25,0,1), 2),
label = c("ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL"),
year = c(2000,2000,2000,2000,2000,2001, 2001, 2001,2001,2001, 2002, 2002,2002,2002,2002,2003,2003,2003,2003,2003,2004,2004,2004,2004,2004))
# Here is one way to do this with ggplot
library(ggplot2)
ggplot(df, aes(x=year, y= score, group = label)) +
geom_line(color='lightgrey')+
geom_line(data = df[df$label=='CHL',], color = 'red') +
geom_text(data = df[df$year == 2000,],mapping =aes(x=1999.5,label=label),color='lightgrey')+
geom_text(data = df[df$year == 2000 & df$label=='CHL',],mapping =aes(x=1999.5,label=label),color='red')+
theme_bw() +
theme(legend.position = 'none')
我正在尝试为我的意大利面条图中的一条线涂上明亮的颜色,以将其与其他线(其他线为灰色)区分开来。在颜色参数中使用一个简单的 ifelse 语句(或创建一个单独的变量 - 都尝试过)应该可以解决问题,但这与正确的国家/地区标签不一致。以国家/地区标签为条件 可以在绘图或标记时正确识别感兴趣的特定国家/地区。但是,在带有 col = 参数的 ifelse 语句中使用它时,突出显示的行会与不正确的国家/地区标签混淆。我想知道以前是否有人遇到过这个问题。非常感谢任何见解。 Reprex 包含在下面。
Data <- data.frame(
score = round(runif(25,0,1), 2),
label = c("ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL"),
year = c(2000,2000,2000,2000,2000,2001, 2001, 2001,2001,2001, 2002, 2002,2002,2002,2002,2003,2003,2003,2003,2003,2004,2004,2004,2004,2004))
library(lattice)
library(directlabels)
## just plotting ZAF label - this is what it should look like
xyplot(Data$score[Data$label == "ZAF"] ~ Data$year[Data$label == "ZAF"], type = "l", xlab = "Year", ylab = "Democracy Score", col = ifelse(Data$label == "ZAF", "red", "grey"))
## now plotting all labels - highlights the wrong label in red, should still highlight ZAF
myplot <- xyplot(Data$score ~ Data$year, group = Data$label, type = "l", xlab = "Year", ylab = "Democracy Score", col = ifelse(Data$label == "ZAF", "red", "grey"))
direct.label(myplot)```
我查看了 here,发现您可以设置 par.settings
。但是您必须按顺序执行此操作,并且标签是按字母顺序排列的。因此,由于 CHL 在您的标签中排在第一位,因此它的颜色需要在 col-vector 中排在第一位。请根据您的喜好更改颜色。
set.seed(123)
df <- data.frame(
score = round(runif(25,0,1), 2),
label = c("ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL"),
year = c(2000,2000,2000,2000,2000,2001, 2001, 2001,2001,2001, 2002, 2002,2002,2002,2002,2003,2003,2003,2003,2003,2004,2004,2004,2004,2004))
library(lattice)
library(directlabels)
myplot <- xyplot(score ~ year,
data = df,
group = label,
auto.key = T,
type = "l",
xlab = "Year", ylab = "Democracy Score",
par.settings = list(
superpose.symbol = list(col = c("red","blue","blue","blue","blue"),pch = 19),
superpose.line = list(col = c("red","blue","blue","blue","blue"),lwd = 2))
)
print(direct.label(myplot))
ggplot中的加法
这里是如何在 ggplot
中完成此操作的示例。这只是实现此目的的众多方法之一。另一种方法是使用包 gghighlight
.
set.seed(123)
df <- data.frame(
score = round(runif(25,0,1), 2),
label = c("ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL"),
year = c(2000,2000,2000,2000,2000,2001, 2001, 2001,2001,2001, 2002, 2002,2002,2002,2002,2003,2003,2003,2003,2003,2004,2004,2004,2004,2004))
# Here is one way to do this with ggplot
library(ggplot2)
ggplot(df, aes(x=year, y= score, group = label)) +
geom_line(color='lightgrey')+
geom_line(data = df[df$label=='CHL',], color = 'red') +
geom_text(data = df[df$year == 2000,],mapping =aes(x=1999.5,label=label),color='lightgrey')+
geom_text(data = df[df$year == 2000 & df$label=='CHL',],mapping =aes(x=1999.5,label=label),color='red')+
theme_bw() +
theme(legend.position = 'none')