ggplot线图,具有不同的线条样式和标记 [英] ggplot line graph with different line styles and markers

查看:3302
本文介绍了ggplot线图,具有不同的线条样式和标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在ggplot2中创建一个线图,该线图将某些变量的不同线条样式和其他变量的不同线标记组合在一起。

不同的线条样式,示例2用不同的标记来绘制每个图形,示例3用不同的线条和标记绘制每个图形。我试图用不同的图形绘制X2和X3线条样式(实线,虚线),然后将X4和X5作为带有不同标记(圆形,方形,等等)的实线。



有没有办法做到这一点??

  library( ggplot2)
library(reshape2)

set.seed < - 1
df < - data.frame(cbind(seq(1,10,1)),matrix rnorm(100,1,20),10,4)))
d< - melt(df,id =X1)

#示例1:不同的线条样式
ggplot(d,aes(x = X1,y = value,color = variable))+
geom_line(aes(linetype = variable),size = 1)

#例2:每条线的不同标记
ggplot(d,aes(x = X1,y = value,color = variable))+
geom_line()+ geom_point(aes(shape = variable,size = 4))

#例3:不同的线条样式&不同的标记(你会看到下面的图)
ggplot(d,aes(x = X1,y = value,color = variable))+
geom_line(aes(linetype = variable),size = 1) +
geom_point(aes(shape = variable,size = 4))

这是一种方法。您可以使用另外两个功能来控制形状和线条类型。 scale_linetype_manual 允许您手动分配线型。同样, scale_shape_manual 允许您手动指定您想要的任何形状。

  #例3:不同的线条样式&不同的标记
ggplot(d,aes(x = X1,y = value,color = variable))+
geom_line(aes(linetype = variable),size = 1)+
geom_point aes(shape = variable,size = 4))+
scale_linetype_manual(values = c(1,2,1,1))+
scale_shape_manual(values = c(0,1,2,3) )


I am trying to create a line graph in ggplot2 that combines different line styles for some variable and different markers for other variables.

Example 1 graphs each variable with a different line style, Example 2 graphs each with a different marker, and Example 3 graphs each with different lines AND markers.

I'm trying to graph X2 and X3 with different line styles (solid, dashed) and then X4 and X5 as solid lines with different markers (circles, square, whatever).

Is there any way to do this??

library(ggplot2)
library(reshape2)

set.seed <- 1
df <- data.frame(cbind(seq(1,10,1),matrix(rnorm(100,1,20), 10, 4)))
d <- melt(df, id="X1")

# Example 1: different line styles
ggplot(d, aes(x=X1, y=value, color=variable)) + 
  geom_line(aes(linetype=variable), size=1)

# Example 2: different markers for each line
ggplot(d, aes(x=X1, y=value, color=variable)) + 
  geom_line() + geom_point(aes(shape=variable, size=4))

# Example 3: differnt line styles & different markers (You see this graph below)
ggplot(d, aes(x=X1, y=value, color=variable)) + 
  geom_line(aes(linetype=variable), size=1) +
  geom_point(aes(shape=variable, size=4))

解决方案

Here is one approach. You can use two more functions to control shape and line type. scale_linetype_manual allows you to manually assign line types. Likewise, scale_shape_manual allows you to manually assigns whichever shape you want.

# Example 3: differnt line styles & different markers
ggplot(d, aes(x=X1, y=value, color=variable)) + 
geom_line(aes(linetype=variable), size=1) +
geom_point(aes(shape=variable, size=4)) +
scale_linetype_manual(values = c(1,2,1,1)) +
scale_shape_manual(values=c(0,1,2,3))

这篇关于ggplot线图,具有不同的线条样式和标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆