2011-03-25 75 views
14

当我有一个多于6个值的变量时,我的麻烦就开始了,因为这是ggplot2中scale_shape函数的当前最大值。如何合并颜色和形状?

由于这个问题,我试着用另一个变量绕过原始变量的长度解决问题。

这里是我的示例代码:

dataf <- structure(list(Municipality = structure(c(2L, 4L, 10L, 11L, 6L, 8L, 3L, 1L, 5L, 9L, 7L), .Label = c("Boyuibe", "Cabezas", "Camiri", "Charagua", "Cuevo", "Gutierrez", "Huacaya", "Lagunillas", "Machareti", "Vallegrande", "Villa Vaca Guzman"), class = "factor"), Growth = c(3.05, 2.85, 0.14, 1.21, 1.59, 2.35, -0.41, 0.81, 0.9, 2.89, 1.8), Density = c(3.0390920594, 0.260984024187, 5.20069847261, 2.50828556783, 3.43964629267, 3.69768961375, 32.4496626479, 2.06145019368, 4.2139578988, 0.740736713557, 1.67034079825)), .Names = c("Municipality", "Growth", "Density"), class = "data.frame", row.names = c(NA, -11L)) 

dataf <- dataf[with(dataf, order(Municipality)), ] 
# create a new column with values 1 to 6 and same length as Municipality 
modulus <- function(x) (x - 1) %% 6 + 1 
indeces <- 1:length(dataf$Municipality) 
dim(indeces) <- length(dataf$Municipality) 
dataf$Shape <- apply(indeces, 1, modulus) 
dataf$Shape <- factor(dataf$Shape, levels=unique(dataf$Shape)) 
plot1 <- ggplot(dataf, aes(x=Density, y=Growth, colour=Municipality, 
     shape=Shape)) 
plot1 <- plot1 + geom_point(size=3) 
plot1 <- plot1 + scale_x_continuous(expression(paste(
     "Population Density [people per km"^2, "]", sep=""))) 
plot1 <- plot1 + scale_y_continuous("Growth Rate [ratio population 2001/
     population 1992]") 
plot1 <- plot1 + scale_colour("Municipality") 
plot1 

产生以下的输出: enter image description here

我想传说是一样的情节点。这是可能的,还是有一个明智的解决方案,我的第一个问题,市长名单太长?

在此先感谢。

+0

请'dput()'文件名的内容,以便有人可以运行您的代码。另外,请参阅CRAN上的[发布指南](http://www.r-project.org/posting-guide.html)以获取获得良好帮助的其他提示。 – Chase 2011-03-25 17:28:28

回答

9

这里有一个例子:

plot1 <- ggplot(dataf, aes(x=Density, y=Growth, colour=Municipality, 
     shape=Municipality)) 
plot1 <- plot1 + geom_point(size=3) 
plot1 <- plot1 + scale_colour_discrete() + 
scale_shape_manual(values=as.numeric(dataf$Shape)) 
plot1 

如果你需要填充的形状,然后用

scale_shape_manual(values=c(16, 17, 15, 3, 7, 8)[as.numeric(dataf$Shape)]) 

更换招数:颜色

  1. 使用相同的变量和形状AES (市)
  2. 使用scale_shape_manual并进行映射o ˚F符(在这里,市)和价值(在这里,DATAF $形状)
  3. 你需要的数值变量,而不是因素的scale_shape_manual
+0

'as.numeric'是我正在寻找的技巧。非常感谢你。我只是将它稍微改为'as.numeric(dataf $ Municipality)'。 – Midnighter 2011-03-26 12:16:33

2

值有关使用scale_shape_manual()什么?如果我正确理解你的问题,你不需要通过颜色和形状来区分,而更喜欢塑造形状,对吧?

ggplot(dataf, aes(x=Density, y=Growth)) + 
    geom_point(aes(shape = Municipality)) + 
    scale_shape_manual(values = 1:11) 

生产: enter image description here

+0

你说得对,形状是主要的特色。不过,@ kohske的回答更加灵活。 – Midnighter 2011-03-26 12:18:19

9

而且招:如果你给无论是传奇的名字,你必须给他们俩相同的名称。如果你只给一个图例命名,ggplot会再次分开图例。修改kohske的例子:

plot1 <- ggplot(dataf, aes(x=Density, y=Growth, colour=Municipality, 
     shape=Municipality)) + geom_point(size=3) 

plot2 <- plot1 + scale_colour_discrete() + 
scale_shape_manual(values=as.numeric(dataf$Municipality)) 

plot2 

plot3 <- plot1 + scale_colour_discrete('City') + 
scale_shape_manual(values=as.numeric(dataf$Municipality)) 

plot3 

plot4 <- plot1 + scale_colour_discrete('City') + 
scale_shape_manual('City',values=as.numeric(dataf$Municipality)) 

plot4 
+0

我不知道是否有任何副作用,但我认为这是最好的解决方案。对我而言,它甚至在没有任何人工分配的情况下与“scale_colour_discrete('City')”和“scale_shape_discrete('City')”一起工作。 – BumbleBee 2016-06-29 10:49:36