首页 > 解决方案 > 您可以通过调用向量中的字符来分配 ggplot2 美学吗?

问题描述

我有一些 R 代码非常适合分析和绘制来自某种类型实验的数据,并且我正在优化它以需要最少的用户输入。不幸的是,当我尝试用向量中的字符分配 ggplot2 美学而不是手动输入它们时,我遇到了障碍。

例如,我想绘制在 22.4 摄氏度记录的数据子集。

isotherms <- {}
isotherms[[1]] <- data.frame(BHQ1 = c(0.0000000, 0.0000000, 0.0000000, 0.8985731, 0.8985731,
                                  0.8985731, 8.9857452, 8.9857452, 8.9857452, 44.9287262,
                                  44.9287262, 44.9287262, 88.4081250, 88.4081250, 88.4081250,
                                  132.6121875, 132.6121875, 132.6121875, 176.8162500, 176.8162500,
                                  176.8162500, 221.0203125, 221.0203125, 221.0203125, 353.6325000,
                                  353.6325000, 353.6325000, 530.4487500, 530.4487500, 530.4487500,
                                  707.2650000, 707.2650000, 707.2650000, 884.0812500, 884.0812500,
                                  884.0812500),
                         Em = c(1.4152309, 1.4838292, 1.5930397, 1.5539322, 1.4728192, 1.5890478,
                                1.3811580, 1.4405066, 1.3924398, 1.2632792, 1.2140788, 1.1582552,
                                0.9606513, 0.8988782, 0.9310566, 0.5657441, 0.5587639, 0.5590144,
                                0.2985498, 0.3017196, 0.3143352, 0.1771695, 0.1862939, 0.1672250,
                                0.1581635, 0.1944296, 0.1562263, 0.1403463, 0.1333305, 0.1357050,
                                0.1207715, 0.1224212, 0.1356236, 0.1317237, 0.1044410, 0.1423820))

我想让我的图例说“22.4 摄氏度”,所以我手动输入了一个符合美学的字符,我得到了一个漂亮的图表:

library(ggplot2)
library(viridis)
pallet <- viridis(2)
ggplot() + geom_point(data = isotherms[[1]], aes(x = BHQ1, y = Em, colour = "22.4"), size = 2) + scale_colour_manual(name = "Temp. (\u00b0C)", values = c("22.4" = pallet[1])) + theme_classic()

带图例的图表

但是,如果我将字符“22.4”放入向量中,然后调用它以在 ggplot2 中分配美学,则代码不起作用。

legend <- c("22.4")

ggplot() + geom_point(data = isotherms[[1]], aes(x = BHQ1, y = Em, colour = legend[1]), size = 2) + scale_colour_manual(name = "Temp. (\u00b0C)", values = c(legend[1] = pallet[1])) + theme_classic()

有谁知道为什么我可以通过键入一个字符来分配美学,但我不能通过从向量中调用一个字符来分配美学?

标签: rggplot2

解决方案


第二个情节并没有因为colour = legend[1]. 它失败是因为c(legend[1] = pallet[1]). 如果您c(legend[1] = pallet[1])在控制台中运行,您可以看到这一点,这会导致:

Error: unexpected '=' in "c(legend[1] ="

您可以通过预先命名向量来解决此问题pallet,例如:

legend <- c("22.4", "30.1")
names(pallet) = legend

然后做:

scale_colour_manual(name = "Temp. (\u00b0C)", values = pallet[1]) +

然而,硬编码美学通常不是最好的方法,因为它破坏了数据和绘图组件之间的自然映射。我不确定您的最终目标是什么,但是,例如,如果您有一个数据框列表,其中每个数据框代表不同温度下的测量值,您可以执行以下操作:

library(tidyverse)

# Add an additional data frame to the isotherms list, just for illustration
isotherms[[2]] = isotherms[[1]] + 2

# Set names of each data frame to the temperature for that isotherm
names(isotherms) = c("22.4", "30.1")

# Combine the two data frames and plot them
bind_rows(isotherms, .id="temp") %>% 
  ggplot(aes(x = BHQ1, y = Em, colour = temp)) + 
    geom_point(size = 2) + 
    scale_colour_manual(name = "Temp. (\u00b0C)", 
                        values = pallet %>% set_names(names(isotherms))) + 
    theme_classic()

在此处输入图像描述


推荐阅读