R:用一个图例绘制线条和符号,然后突出显示一些数据

时间:2014-12-22 17:44:12

标签: r plot ggplot2 legend

我想要做的是用线条和点绘制一些数据,颜色,线条类型和点样式各不相同。然后我想绘制具有与第一个绘图相同属性的数据子集。然后,使用数据子集制作第三个图,并以灰色显示未选择的数据。

我还希望这三个图的图例宽度相同,并且有10列。

这是我的“尝试”

# Create sample data
set.seed(1)
nSamples=1000
dt<-data.table(src=sample(paste('SRC-',11:25,sep=''),nSamples,replace=TRUE),
               id=sample(1001:1150,nSamples,replace=TRUE),
               dhr=sample(seq(.1,2,.1),nSamples,replace=TRUE),
               dcnt=sample(1:5,nSamples,replace=TRUE))
dt$hr<-ave(dt$dhr,dt$id,FUN=cumsum) 
dt$cnt<-ave(dt$dcnt,dt$id,FUN=cumsum) 

# Set plot characteristics
dt$linetype <- factor((dt$id %% 4)+1)
dt$color <- factor(dt$id)
dt$shape <- factor((dt$id %% 6)+1)

# Question 1: How to get the legend to correspond to the plot (color, linetype, symbol)
# and have 10 columns
ggplot(dt,aes(x=hr,y=cnt,group=id,color=color,linetype=linetype))+
  geom_line() +
  geom_point(aes(shape=dt$shape))+
  ggtitle('All ID') +
  xlab("Time (hr)") +
  ylab("Repair (Part count)") +
  guides(color=guide_legend(ncol=10,title='ID'))

# Question 2: Select some source data to be plotted.
# How to plot with same 'colored' lines / symbols as plot 1
srcIndex <- grep('SRC-14',dt$src)
srcData<-dt[srcIndex,]
nonsrcData<-dt[-srcIndex,]

ggplot(data=NULL,aes(x=hr,y=cnt,group=id,color=color,linetype=linetype))+
#  geom_line(data=nonsrcData, color='grey') +
#  geom_point(data=nonsrcData, color='grey', shape=nonsrcData$shape)+
  ggtitle('SRC-14 ID') +
  xlab("Time (hr)") +
  ylab("Repair (Part count)") +
  geom_line(data=srcData) +
  geom_point(data=srcData,aes(shape=srcData$shape))+
  guides(color=guide_legend(ncol=10,title='ID'))

# Question 3: 
# How to plot the nonsource  plot in part 1 with 'grey' lines / symbols, and then 
# overlay with 'colored' lines / symbols

ggplot(data=NULL,aes(x=hr,y=cnt,group=id,color=color,linetype=linetype))+
  geom_line(data=nonsrcData, color='grey') +
  geom_point(data=nonsrcData, color='grey', shape=nonsrcData$shape)+
  ggtitle('SRC-14 ID') +
  xlab("Time (hr)") +
  ylab("Repair (Part count)") +
  geom_line(data=srcData) +
  geom_point(data=srcData,aes(shape=srcData$shape))+
  guides(color=guide_legend(ncol=10,title='ID'))

此图需要更正图例,以便单个图例显示线型和颜色的变化以及符号的变化,符号颜色与线颜色匹配。另外,删除两个额外的图例。

enter image description here

该图与图1有相同的问题,带有图例。比例应与图1相同。 enter image description here

第三个数字应该与第二个相似,其他数据加灰色。 enter image description here

1 个答案:

答案 0 :(得分:1)

以下代码使用正确的图例生成3个图。

set.seed(1)
nSamples=1000
dt<-data.table(src=sample(paste('SRC-',11:25,sep=''),nSamples,replace=TRUE),
               id=sample(1001:1150,nSamples,replace=TRUE),
               dhr=sample(seq(.1,2,.1),nSamples,replace=TRUE),
               dcnt=sample(1:5,nSamples,replace=TRUE))
dt$hr<-ave(dt$dhr,dt$id,FUN=cumsum) 
dt$cnt<-ave(dt$dcnt,dt$id,FUN=cumsum) 

# Set plot characteristics
dt$linetype <- factor((dt$id %% 4)+1)
dt$color <- factor(dt$id)
dt$shape <- factor((dt$id %% 6)+1)

# Question 1: How to get the legend to correspond to the plot (color, linetype, symbol)
# and have 10 columns
dt$id <- factor(dt$id)
sortid<-order(dt$id)
unik <- which(!duplicated(dt$id[sortid]))
unik <- sortid[unik]

ggplot(dt, aes(hr, cnt, group = id)) + 
  geom_line(size=1, aes(color=dt$color, linetype=dt$linetype)) +
  geom_point(data = dt, 
             aes(hr, cnt, group = id, color = id, shape=shape),size=3)  +
  guides(colour = guide_legend(ncol=10,title='ID',
                               override.aes = list(shape = as.integer(dt$shape[unik]),
                                                   linetype = as.integer(dt$linetype[unik]))),
         shape = FALSE, linetype = FALSE) +
  ggtitle('ALL ID') +
  xlab("Time (hr)") +
  ylab("Repair (Part count)") +
  xlim(0, 25) + ylim(0,60)

# Question 2: Select some source data to be plotted.
# How to plot with same 'colored' lines / symbols as plot 1
srcIndex <- grep('SRC-14',dt$src)
srcData<-dt[srcIndex,]
nonsrcData<-dt[-srcIndex,]

sort_src_id<-order(srcData$id)
unik_src <- which(!duplicated(srcData$id[sort_src_id]))
unik_src <- sort_src_id[unik_src]

ggplot(srcData, aes(hr, cnt, group = id)) + 
  geom_line(size=1, aes(color=srcData$color, linetype=srcData$linetype)) +
  geom_point(data = srcData, 
             aes(hr, cnt, group = id, color = id, shape=shape),size=3)  +
  guides(colour = guide_legend(ncol=10,title='ID',
                               override.aes = list(shape = as.integer(srcData$shape[unik_src]),
                                                   linetype = as.integer(srcData$linetype[unik_src]))),
         shape = FALSE, linetype = FALSE) +
  ggtitle('SRC-14') +
  xlab("Time (hr)") +
  ylab("Repair (Part count)") +
  xlim(0, 25) + ylim(0,60) 


# Question 3: 
# How to plot the nonsource  plot in part 1 with 'grey' lines / symbols, and then 
# overlay with 'colored' lines / symbols

ggplot() + 
  geom_line(size=.5, color='grey', aes(x=nonsrcData$hr,
                                       y=nonsrcData$cnt,
                                       linetype=nonsrcData$linetype,
                                       group=nonsrcData$id)) +
  geom_point(size=1,color='grey',aes(x=nonsrcData$hr,
                                     y=nonsrcData$cnt,
                                     shape=nonsrcData$shape,
                                     group=nonsrcData$id)) +
  geom_line(data=srcData, size=1.5, aes(hr, cnt, 
                        group = id,
                        color=color, linetype=linetype)) +
  geom_point(data = srcData, size=3,
             aes(hr, cnt, group = id, color = color, shape=shape))  +
  guides(colour = guide_legend(ncol=10,title='ID',
                               override.aes = list(shape = as.integer(srcData$shape[unik_src]),
                                                   linetype = as.integer(srcData$linetype[unik_src]))),
         shape = FALSE, linetype = FALSE) +
  ggtitle('SRC-14') +
  xlab("Time (hr)") +
  ylab("Repair (Part count)") +
  xlim(0, 25) + ylim(0,60) 

上面的代码生成了所有3个图。这是第一个显示所有数据,更正了图例。

enter image description here

这是第三个图表,显示所选子集突出显示,并更正了图例 enter image description here