【问题标题】:Scatter plot for multiple variables on x axisx 轴上多个变量的散点图
【发布时间】:2020-04-02 15:01:11
【问题描述】:

我想为不同列中定义的多个变量创建散点图。 样本输入:

df=structure(list(section = structure(1:6, .Label = c("a", "b", 
"c", "d", "e", "f"), class = "factor"), level = c(0L, 1L, 2L, 
1L, 2L, 0L), math.av = c(83L, 67L, 76L, 56L, 72L, 76L), science.av = c(75L, 
76L, 82L, 72L, 82L, 68L), language.av = c(80L, 75L, 80L, 73L, 
85L, 70L)), class = "data.frame", row.names = c(NA, -6L))

我希望将 x 轴划分为受试者的 3 个不同列中的每一列,并且 y 轴表示它们的不同值。

我必须用另一列作为指标将它们按颜色分组,在这种情况下,根据级别

【问题讨论】:

  • 请提供一个最小的例子。
  • 另外,请考虑分面,以减少混乱
  • @FrancescoGrossetti 更新了我的问题

标签: r scatter-plot categorical-data


【解决方案1】:

这是dplyrggplot2 的一种方法。

首先,我们需要将数据转为长而不是宽。

library(dplyr)
library(tidyr)
long_df <- df %>% pivot_longer(-c(section,level))

然后我们可以绘制一个点图。

library(ggplot2)
ggplot(long_df, aes(x=as.factor(name),y=value,fill=as.factor(level))) +
  geom_dotplot(binaxis='y') +
  labs(x="Subject") +
  guides(fill = guide_legend(title = "Level"))

数据

df <- structure(list(section = structure(1:6, .Label = c("a", "b", 
"c", "d", "e", "f"), class = "factor"), level = c(0L, 1L, 2L, 
1L, 2L, 0L), math.av = c(83L, 67L, 76L, 56L, 72L, 76L), science.av = c(75L, 
76L, 82L, 72L, 82L, 68L), language.av = c(80L, 75L, 80L, 73L, 
85L, 70L)), class = "data.frame", row.names = c(NA, -6L))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 2020-11-05
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多