如何按数字顺序放置图例名称

发布于 2025-01-11 14:30:31 字数 806 浏览 0 评论 0原文

我正在处理以数字结尾的字符串,并希望将字符串按数字图例的数字顺序排列。

# Example dataframe
d <- data.frame(
  SampleID = sprintf("sample_%s", rep(10:39)),
  Variable = 1:30,
  Group = append(rep("group_1", each = 15), rep("group_2", each = 15)),
  Group1 = runif(30, min=0, max=100),
  Group2 = runif(30, min=0, max=100)
)

# Character level I add to give each animal unique shape
d$Animals <- sprintf("Animal_%s",rep(c(1:10), each = 3)) 

library(ggplot2)

# Plot
ggplot(d, aes(Group1, Group2, color=Group, shape=Animals)) +
  xlab(paste0("Value 1")) +
  ylab(paste0("Value 2")) + 
  coord_fixed() +
  geom_point() +
  scale_shape_manual(values=c(0:10))

我当前的输出是这样的 输入图片此处描述

I am working with strings that end with a number and would like to put the strings in numerical order for the figure legend.

# Example dataframe
d <- data.frame(
  SampleID = sprintf("sample_%s", rep(10:39)),
  Variable = 1:30,
  Group = append(rep("group_1", each = 15), rep("group_2", each = 15)),
  Group1 = runif(30, min=0, max=100),
  Group2 = runif(30, min=0, max=100)
)

# Character level I add to give each animal unique shape
d$Animals <- sprintf("Animal_%s",rep(c(1:10), each = 3)) 

library(ggplot2)

# Plot
ggplot(d, aes(Group1, Group2, color=Group, shape=Animals)) +
  xlab(paste0("Value 1")) +
  ylab(paste0("Value 2")) + 
  coord_fixed() +
  geom_point() +
  scale_shape_manual(values=c(0:10))

My current output is this
enter image description here

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

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

发布评论

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

评论(3

南渊 2025-01-18 14:30:31

您可以将 Animal 变量转换为有序且水平的因子

# Example dataframe
d <- data.frame(
  SampleID = sprintf("sample_%s", rep(10:39)),
  Variable = 1:30,
  Group = append(rep("group_1", each = 15), rep("group_2", each = 15)),
  Group1 = runif(30, min=0, max=100),
  Group2 = runif(30, min=0, max=100)
)

# Character level I add to give each animal unique shape
d$Animals <- sprintf("Animal_%s",rep(c(1:10), each = 3)) 


d <- d %>% 
  mutate(Animals = factor(Animals, 
                          levels = c( "Animal_1" , "Animal_2" , "Animal_3" , "Animal_4",  "Animal_5" ,
  "Animal_6" , "Animal_7",  "Animal_8" , "Animal_9"  ,"Animal_10"), 
  ordered = T))
library(ggplot2)

# Plot
ggplot(d, aes(Group1, Group2, color=Group, shape=Animals)) +
  xlab(paste0("Value 1")) +
  ylab(paste0("Value 2")) + 
  coord_fixed() +
  geom_point() +
  scale_shape_manual(values=c(0:10))

sample

You can make the convert the Animal variable into an ordered and leveled factor

# Example dataframe
d <- data.frame(
  SampleID = sprintf("sample_%s", rep(10:39)),
  Variable = 1:30,
  Group = append(rep("group_1", each = 15), rep("group_2", each = 15)),
  Group1 = runif(30, min=0, max=100),
  Group2 = runif(30, min=0, max=100)
)

# Character level I add to give each animal unique shape
d$Animals <- sprintf("Animal_%s",rep(c(1:10), each = 3)) 


d <- d %>% 
  mutate(Animals = factor(Animals, 
                          levels = c( "Animal_1" , "Animal_2" , "Animal_3" , "Animal_4",  "Animal_5" ,
  "Animal_6" , "Animal_7",  "Animal_8" , "Animal_9"  ,"Animal_10"), 
  ordered = T))
library(ggplot2)

# Plot
ggplot(d, aes(Group1, Group2, color=Group, shape=Animals)) +
  xlab(paste0("Value 1")) +
  ylab(paste0("Value 2")) + 
  coord_fixed() +
  geom_point() +
  scale_shape_manual(values=c(0:10))

sample

剑心龙吟 2025-01-18 14:30:31

如果因子数量太大,则接受的解决方案不可行,因为它需要以正确的顺序手动输入因子级别。如果原始数据排序不正确,建议的另一个解决方案(使用 unique(d$Animals) 以正确的顺序获取级别)也可能会失败。如果有人需要更通用的解决方案(这是一个非常常见的问题),您可以使用

d$Animals <- factor(sprintf("Animal_%s",rep(c(1:10), each = 3)))

d$Animals <- reorder(d$Animals,
  as.numeric(gsub("^[^_]*_(\\d+).*", "\\1", d$Animals)))

这将根据字符串中找到的第一个数字正确地重新排序级别,无论是 10 还是 1000 个级别以及它们在数据集中出现的顺序。

The accepted solution is not viable if the number of factors is too large as it requires manually typing the factor levels in the correct order. The other solution proposed (using unique(d$Animals) to get the levels in the correct order) also may fail if the original data is incorrectly ordered. In case someone needs a more general solution (this is a very common problem) you can use

d$Animals <- factor(sprintf("Animal_%s",rep(c(1:10), each = 3)))

d$Animals <- reorder(d$Animals,
  as.numeric(gsub("^[^_]*_(\\d+).*", "\\1", d$Animals)))

This will properly reorder the levels according to the first number found in the string regardless of being 10 or 1000 levels and the order they appear in your dataset.

旧故 2025-01-18 14:30:31

谢谢@Susan Switzer,我会接受这个答案,但是我对你原来的解决方案做了一些小小的改变。

d <- data.frame(
  SampleID = sprintf("sample_%s", rep(10:39)),
  Variable = 1:30,
  Group = append(rep("group_1", each = 15), rep("group_2", each = 15)),
  Group1 = runif(30, min=0, max=100),
  Group2 = runif(30, min=0, max=100)
)


d$Animals <- sprintf("Animal_%s",rep(c(1:10), each = 3)) 


library(dplyr)

d <- d %>% 
  mutate(Animals = factor(Animals, 
                          levels = unique(d$Animals), 
                          ordered = T))

library(ggplot2)

ggplot(d, aes(Group1, Group2, color=Group, shape=Animals)) +
  xlab(paste0("Value 1")) +
  ylab(paste0("Value 2")) + 
  coord_fixed() +
  geom_point() +
  scale_shape_manual(values=c(0:10))

输入图片此处描述

Thank you @Susan Switzer, I'll accept this answer, however I made a slight change to your original solution.

d <- data.frame(
  SampleID = sprintf("sample_%s", rep(10:39)),
  Variable = 1:30,
  Group = append(rep("group_1", each = 15), rep("group_2", each = 15)),
  Group1 = runif(30, min=0, max=100),
  Group2 = runif(30, min=0, max=100)
)


d$Animals <- sprintf("Animal_%s",rep(c(1:10), each = 3)) 


library(dplyr)

d <- d %>% 
  mutate(Animals = factor(Animals, 
                          levels = unique(d$Animals), 
                          ordered = T))

library(ggplot2)

ggplot(d, aes(Group1, Group2, color=Group, shape=Animals)) +
  xlab(paste0("Value 1")) +
  ylab(paste0("Value 2")) + 
  coord_fixed() +
  geom_point() +
  scale_shape_manual(values=c(0:10))

enter image description here

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