簡體   English   中英

x 軸上多個變量的散點圖

[英]Scatter plot for multiple variables on x axis

我想為不同列中定義的多個變量創建一個散點圖。 樣本輸入:

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

我希望為主題的 3 個不同列中的每一列划分 x 軸,而 y 軸代表它們的不同值。

我必須將它們與另一列的顏色分組作為指標,在這種情況下,根據級別

這是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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM