如何融合颜色和形状?

发布于 2024-10-25 23:14:59 字数 1717 浏览 2 评论 0原文

当我有一个超过 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

产生以下输出: 在此处输入图像描述

我希望图例与图中的点一样。这可能吗?或者对于我的第一个问题(城市列表太长)有一个聪明的解决方案吗?

提前致谢。

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 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

that produces the following output:
enter image description here

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如梦亦如幻 2024-11-01 23:14:59

这是一个示例:

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并制作中断(此处为市政当局)和值(此处为dataf$Shape)的映射
  3. 您需要数字变量而不是 scale_shape_manual 值的因子

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))
plot1

if you need filled shapes, then replace with

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

the tricks are:

  1. use same variable for colour and shape aes (Municipality)
  2. use scale_shape_manual and make mapping of breaks (here, Municipality) and value (here, dataf$Shape)
  3. you need numeric variable instead of factor for values of scale_shape_manual
别闹i 2024-11-01 23:14:59

进一步的技巧:如果您为任一图例命名,则必须为它们指定相同的名称。如果您只给一个图例命名,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

Further trick: If you give either legend a name, you must give them both the same name. If you give only one legend a name, ggplot will separate the legends again. Amending kohske's example:

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
好倦 2024-11-01 23:14:59

使用scale_shape_manual()怎么样?如果我正确理解你的问题,你真的不需要通过颜色和形状来区分,而是更喜欢形状,对吗?

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

产生:
在此处输入图像描述

What about using scale_shape_manual()? If I understood your question correctly, you don't really need to differentiate by both colour and shape and would prefer shape, right?

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

produces:
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文