R:将两个系列添加到图形中

R: Adding Two Series to a Graph

使用以下网站 (http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html),我制作了下图:

mtcars$`car name` <- rownames(mtcars)  # create new column for car names
mtcars$mpg_z <- round((mtcars$mpg - mean(mtcars$mpg))/sd(mtcars$mpg), 2)  # compute normalized mpg
mtcars$mpg_type <- ifelse(mtcars$mpg_z < 0, "below", "above")  # above / below avg flag
mtcars <- mtcars[order(mtcars$mpg_z), ]  # sort
mtcars$`car name` <- factor(mtcars$`car name`, levels = mtcars$`car name`)  # convert to factor to retain sorted order in plot.

library(ggplot2)
theme_set(theme_bw())

# Plot
ggplot(mtcars, aes(x=`car name`, y=mpg_z, label=mpg_z)) + 
  geom_point(stat='identity', aes(col=mpg_type), size=6)  +
  scale_color_manual(name="Mileage", 
                     labels = c("Above Average", "Below Average"), 
                     values = c("above"="#00ba38", "below"="#f8766d")) + 
  geom_text(color="white", size=2) +
  labs(title="Diverging Dot Plot", 
       subtitle="Normalized mileage from 'mtcars': Dotplot") + 
  ylim(-2.5, 2.5) +
  coord_flip()

我的问题:我想修改上图,使每条水平线上有“2个点”(绿色和红色),代表两个不同变量的值.

我为此示例创建了一个数据集:

my_data = data.frame(var_1_col = "red", var_2_col = "green", var_1 = rnorm(8,10,10), var_2 = rnorm(8,5,1), name = c("A", "B", "C", "D", "E", "F", "G", "H"))

  var_1_col var_2_col     var_1    var_2 name
1       red     green 14.726642 4.676161    A
2       red     green 11.011187 4.937376    B
3       red     green 12.418489 5.869617    C
4       red     green 21.935154 5.641106    D
5       red     green 20.209498 6.193123    E
6       red     green -5.339944 5.187093    F
7       red     green 20.540806 3.895683    G
8       red     green 21.619631 4.097438    H

然后,我尝试创建图表 - 但结果是空的:

# Plot
ggplot(my_data, aes(x=name, y=var_1, label=name)) + 
  geom_point(stat='identity', aes(col=var_1_col), size=6)  +
  scale_color_manual(name="Var 1 or Var 2", 
                     labels = c("Var 1", "Var 2"), 
                     values = c("Var 1"="#00ba38", "Var 2"="#f8766d")) + 
  geom_text(color="white", size=2) +
  labs(title="Plot", 
       subtitle="Plot: Dotplot") + 
  ylim(-2.5, 2.5) +
  coord_flip()

理想情况下,我希望图表看起来像这样:

有人可以告诉我怎么做吗?

谢谢!

注意: var_1 可能是“平均燃油价格”之类的变量,var_2 可能是“中位数燃油价格”

I want to modify [...], representing the values of two different variables.

如果您希望在同一图表上绘制两个不同的变量(并且它们共享一个公共轴,就像本例中的名称),您可以构造两个单独的 geom_point 参数。

ggplot(my_data) +
  geom_point(aes(x=name, y=var_1, col=var_1_col)) +
  geom_point(aes(x=name, y=var_2, col=var_2_col)) +
  coord_flip()

您不必总是在初始 ggplot 函数中定义 axes/colors/labels。通过仅指定数据集,您可以灵活地使用在以下特定于图形的函数中使用的变量。这就是你如何在一个图上构建多个图:)

我建议将数据放入长格式,因为这是用 ggplot2 绘图时的首选。所以,我将删除两个颜色列,因为您可以在 scale_color_manual 中设置它。然后,在 aes for geom_point 中,我们可以设置我们希望两个变量的颜色不同(即,作为它们自己的组)。然后,我们仍然可以在 scale_color_manual.

中设置所有的标签、名称和颜色
library(tidyverse)

my_data %>%
  select(-c(var_1_col, var_2_col)) %>%
  pivot_longer(-name, names_to = "variable", values_to = "value") %>%
  ggplot(., aes(x = name, y = value, label = name)) +
  geom_point(stat = 'identity', aes(color = variable), size = 6)  +
  scale_color_manual(
    name = "Var 1 or Var 2",
    labels = c("Var 1", "Var 2"),
    values = c("#00ba38", "#f8766d")
  ) +
  labs(title = "Plot",
       subtitle = "Plot: Dotplot") +
  coord_flip() +
  theme_bw()

输出