use*_*321 62 plot r unique lines colors
我是R的新手,我有以下疑问:
我试图在R中生成一个有多行(数据系列)的图.这些行中的每一行都是一个类别,我希望它具有独特的颜色.
目前我的代码是以这种方式设置的:
首先,我正在创建一个空图:
plot(1,type='n',xlim=c(1,10),ylim=c(0,max_y),xlab='ID', ylab='Frequency')
Run Code Online (Sandbox Code Playgroud)
然后对于我的每个类别,我使用"for"循环在这个空图中绘制线条,如下所示:
for (category in categories){
lines(data.frame.for.this.category, type='o', col=sample(rainbow(10)), lwd=2)
}
Run Code Online (Sandbox Code Playgroud)
这里有8个类别,因此该图中有8个线.正如您所看到的,我尝试从rainbows()函数中采样颜色以生成每行的颜色.
但是,当生成绘图时,我发现有多条线具有相同的颜色.例如,这8行中的3行具有绿色.
如何使这8条线中的每一条都具有独特的颜色?
另外,我如何在情节的传说中反映这种独特性?我试图查找legend()
函数,但不清楚我应该使用哪个参数来反映每个类别的这种独特颜色?
任何帮助或建议将不胜感激.
use*_*1_G 68
如果你的数据是宽格式的, 那就matplot
常常被遗忘:
dat <- matrix(runif(40,1,20),ncol=4) # make data
matplot(dat, type = c("b"),pch=1,col = 1:4) #plot
legend("topleft", legend = 1:4, col=1:4, pch=1) # optional legend
Run Code Online (Sandbox Code Playgroud)
酒店还为那些不熟悉的东西,如额外的奖励ggplot
,大部分绘制paramters如pch
等使用相同matplot()
的plot()
.
Aru*_*run 46
如果您想要一个ggplot2
解决方案,如果您可以将数据调整为这种格式,则可以执行此操作(请参阅下面的示例)
# dummy data
set.seed(45)
df <- data.frame(x=rep(1:5, 9), val=sample(1:100, 45),
variable=rep(paste0("category", 1:9), each=5))
# plot
ggplot(data = df, aes(x=x, y=val)) + geom_line(aes(colour=variable))
Run Code Online (Sandbox Code Playgroud)
jor*_*ran 26
你有使用基本图形的正确的一般策略,但正如所指出的那样,你实际上是在告诉R为每一行从一组10中挑选一个随机颜色.鉴于此,您偶尔会得到两条颜色相同的线并不奇怪.这是使用基本图形的示例:
plot(0,0,xlim = c(-10,10),ylim = c(-10,10),type = "n")
cl <- rainbow(5)
for (i in 1:5){
lines(-10:10,runif(21,-10,10),col = cl[i],type = 'b')
}
Run Code Online (Sandbox Code Playgroud)
注意使用type = "n"
来抑制原始调用中的所有绘图来设置窗口,以及cl
for循环内部的索引.
Sha*_*fiq 12
使用该lines()
功能可以在同一图表上绘制多条线
# Create the data for the chart.
v <- c(7,12,28,3,41)
t <- c(14,7,6,19,3)
# Give the chart file a name.
png(file = "line_chart_2_lines.jpg")
# Plot the bar chart.
plot(v,type = "o",col = "red", xlab = "Month", ylab = "Rain fall",
main = "Rain fall chart")
lines(t, type = "o", col = "blue")
# Save the file.
dev.off()
Run Code Online (Sandbox Code Playgroud)
使用@Arun虚拟数据:)这里有一个lattice
解决方案:
xyplot(val~x,type=c('l','p'),groups= variable,data=df,auto.key=T)
Run Code Online (Sandbox Code Playgroud)
小智 6
我知道,它过去是一则要回答的帖子,但就像我偶然发现相同的帖子一样,其他人也可能会转过来
通过在ggplot函数中添加:color,我可以实现与绘图中存在的组相关的具有不同颜色的线。
ggplot(data=Set6, aes(x=Semana, y=Net_Sales_in_pesos, group = Agencia_ID, colour = as.factor(Agencia_ID)))
Run Code Online (Sandbox Code Playgroud)
和
geom_line()
Run Code Online (Sandbox Code Playgroud)
@joran的答案除了将base plot
函数与for
循环一起使用外,还可以将base plot
与lapply
以下代码一起使用:
plot(0,0,xlim = c(-10,10),ylim = c(-10,10),type = "n")
cl <- rainbow(5)
invisible(lapply(1:5, function(i) lines(-10:10,runif(21,-10,10),col = cl[i],type = 'b')))
Run Code Online (Sandbox Code Playgroud)
invisible
函数只是用来防止lapply
在控制台中产生列表输出(因为我们想要的只是该函数提供的递归,而不是列表)。如您所见,它产生与使用for
循环方法完全相同的结果。
那为什么要使用lapply
呢?
尽管lapply
已显示出比for
R 更快/更好的性能(例如,请参见此处;尽管在此处看到的情况并非如此),但在这种情况下,其性能大致相同:
lapply
和for
方法的线数都增加到50000 ,分别花费了我的系统46.3
和46.55
几秒钟的时间。
lapply
速度稍快,但可以忽略不计。对于更大/更复杂的图形,这种速度差异可能会派上用场,但是老实说,50000行可能是一个相当不错的上限...因此,“为什么lapply
?” 的答案:这只是一种同样有效的替代方法。:)