2013-02-13 74 views
52

我是相当新的R和我有以下疑问独特的色彩:情节多行(数据系列),每个R中

我试图产生R A曲线图,有多条线路(数据系列) 。每一行都是一个类别,我希望它具有独特的颜色。

目前我的代码是建立在这样:

首先,我创建一个空的情节:

plot(1,type='n',xlim=c(1,10),ylim=c(0,max_y),xlab='ID', ylab='Frequency') 

然后我的每个类别的,我策划在使用这个空的情节线索“for”循环如下:

for (category in categories){ 
lines(data.frame.for.this.category, type='o', col=sample(rainbow(10)), lwd=2) 
} 

这里有8类,所以这里有8条生产线。正如你所看到的,我试图从rainbows()函数中抽取一个颜色来为每一行生成一个颜色。

但是,当绘图生成时,我发现有多条线具有相同的颜色。例如,这8条线中的3条线具有绿色。

如何让这8条线中的每一条都具有独特的颜色?

另外,如何在情节的传说中体现这种独特性?我试图查找legend()函数,但是不清楚应该使用哪个参数来反映每个类别的这种独特颜色?

任何帮助或建议将不胜感激。

+2

您可能想要更改col = category,那么您可能会看到每个系列的不同颜色。你能给我们提供样品数据吗? ggplot2可以是一个更容易的选择。 – 2013-02-13 18:09:43

回答

41

如果你想一个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)) 

ggplot2_geom_line

21

您有做正确的总体战略这是使用基础图形,但正如你指出的,你基本上告诉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') 
} 

enter image description here

注意使用type = "n"抑制原始呼叫所有策划设立窗口,并cl内的索引for循环。

+0

(Imo)R初学者的极佳解决方案:) – 2014-06-19 16:45:43

58

如果你的数据在wide formatmatplot制成,这和经常忘记:

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 

还为那些不熟悉的事情像ggplot额外的奖励,大部分的策划paramters如pch等。使用matplot()plot()相同。 enter image description here

+1

非常感谢您指出这一点! – SirRichie 2015-03-03 16:42:24

7

使用@Arun虚拟数据:)这里lattice解决方案:

xyplot(val~x,type=c('l','p'),groups= variable,data=df,auto.key=T) 

enter image description here

1

这是一个示例代码,如果感兴趣的话,它包含一个图例。

# First create an empty plot. 
plot(1, type = 'n', xlim = c(xminp, xmaxp), ylim = c(0, 1), 
    xlab = "log transformed coverage", ylab = "frequency") 

# Create a list of 22 colors to use for the lines. 
cl <- rainbow(22) 

# Now fill plot with the log transformed coverage data from the 
# files one by one. 
for(i in 1:length(data)) { 
    lines(density(log(data[[i]]$coverage)), col = cl[i]) 
    plotcol[i] <- cl[i] 
} 
legend("topright", legend = c(list.files()), col = plotcol, lwd = 1, 
     cex = 0.5) 
2

我知道,它的老后回答,但就像我整个搜索同一岗位来了,别人可能会转向这里也

通过添加:颜色ggplot功能,我可以实现与图中出现的组有关的不同颜色的线。

ggplot(data=Set6, aes(x=Semana, y=Net_Sales_in_pesos, group = Agencia_ID, colour = as.factor(Agencia_ID)))  

geom_line() 

Line Graph with Multiple colors

3

一个以上的线可以在同一图表上通过使用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() 

OUTPUT enter image description here

被吸入