本文介绍了如何合并颜色和形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 当我有一个变量的值超过6时,我的麻烦开始了,因为这是ggplot2中scale_shape函数的当前最大值。 由于这个问题,我试着用另一个变量绕过原始变量的长度解决问题。 这是我的示例代码: $ (结构(列表(城市)=结构(c(2L,4L,10L,11L,6L,8L,3L,1L,5L,9L,7L) ),.Label = c(Boyuibe,Cabezas,Camiri,Charagua,Cuevo,Gutierrez,Huacaya,Lagunillas,Machareti,Vallegrande (3.05,2.85,0.14,1.21,1.59,2.35,-0.41,0.81,0.9,2.89,1.8),密度= c(3.0390920594,0.260984024187,5.20069847261,2.50828556783),密度= ,3.43964629267,3.696868961375,32.4496626479,2.06145019368,4.2139578988,0.740736713557,1.67034079825)),.Names = c(自治市,增长,密度),class =data.frame,row.names = c(NA ,-11L)) dataf< - dataf [with(dataf,order(Municipality)),] #创建一个新的列,其值为1到6,长度与Municipality modulus indeces dim(indeces)< - 长度(dataf $ Municipality) dataf $形状< - apply(indeces,1,modulus) dataf $ Shape plot1 形状=形状)) plot1 plot1 Population Density [people per km^ 2,],sep =))) plot1< - plot1 + scale_y_continuous(Growth Rate [ratio population 2001 / 人口1992]) plot1 plot1 产生以下输出: 我希望这个传说就像情节中的点。这是可能的,还是有一个明智的解决方案,我的第一个问题,市长名单太长? 预先感谢。 plot1 解决方案 ggplot(dataf,aes(x = Density,y = Growth,color = Municipality, shape = Municipality)) plot1 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)]) 技巧是: $ b 对颜色和形状使用相同的变量aes(自治市) 使用scale_shape_manual和使断点(这里,市政)和价值(这里,dataf $形状)的映射 您需要数字变量scale_shape_manual 的价值因素My troubles started when I had a variable with more than 6 values because that is the current maximum value for the scale_shape function in ggplot2.Due to that problem I tried a work-around with another variable that I just wrapped around the length of the original variable.Here is my example code: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 Municipalitymodulus <- function(x) (x - 1) %% 6 + 1indeces <- 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")plot1that produces the following output:I would like the legend to be just like the points in the plot. Is that possible, or is there a smart solution to my first problem with the list of municipalities being too long?Thanks in advance. 解决方案 here is an example: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))plot1if you need filled shapes, then replace withscale_shape_manual(values=c(16, 17, 15, 3, 7, 8)[as.numeric(dataf$Shape)])the tricks are:use same variable for colour and shape aes (Municipality)use scale_shape_manual and make mapping of breaks (here, Municipality) and value (here, dataf$Shape)you need numeric variable instead of factor for values of scale_shape_manual 这篇关于如何合并颜色和形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-20 20:35