簡體   English   中英

ggplot2中一個變量的縮放點

[英]Scaling points of one variable in ggplot2

我正在使用 ggplot 繪制測驗問題的性能數據。 測驗有兩組問題,隨機和強制性。 下圖顯示了兩組都根據這些問題的嘗試次數來衡量。 但是,這兩個組都在縮放,我只希望隨機 30 組縮放到嘗試的大小。 我還在下面提供了我的代碼的一部分,它將擴展這兩個組(我無法在此處添加數據,因為它包含機密信息)。 先感謝您!

平均分數與點雙列圖,兩個變量都按比例縮放

ggplot(weekData, aes(x=firstPB, y=firstAverageQScore))+
  geom_point(aes(colour=section, size=questionAttempt))+
  labs(size="Number of attempts")+
  geom_smooth(method = glm, se= F) +
  ggtitle(paste0("Week ",week," First Attempts")) +
  xlab("Point Biserial") + xlim(-1,1)+
  ylab("Average Score for Question(%)") + ylim(0,100) +
  geom_label_repel(aes(label=number), alpha=0.5,max.overlaps = Inf)+
  geom_vline(xintercept = 0, linetype="dashed",
             color = "black", size=0.5)+
  scale_colour_discrete(name="Group",
                       breaks=c("random", "mandatory"),
                       labels=c("Random 30","Mandatory 10")) +
  theme(panel.background = element_rect(fill = "white", 
                                        colour = "white",
                                        size = 0.5, linetype = "solid")) +
  annotate("rect", xmin=(0.25), xmax=(Inf), 
           ymin=(30), ymax=(70), fill="#b1b3b133")

我將破解一些虛假數據以嘗試解決方案:

weekData <- data.frame(section = c("random", "mandatory"), 
                       firstPB = c(0,0.1,0.2,0.3), 
                       firstAverageQScore = c(80, 85), 
                       questionAttempt = c(10, 10, 60, 60), 
                       number = 1:4)

#    section firstPB firstAverageQScore questionAttempt number
#1    random     0.0                 80              10      1
#2 mandatory     0.1                 85              10      2
#3    random     0.2                 80              60      3
#4 mandatory     0.3                 85              60      4

week = 10  # used in title

解決此問題的一種方法是使用兩個層進行顯示,每個層都可以看到不同的數據子集。 一個映射questionAttempt嘗試調整大小,另一個映射大小硬編碼:

ggplot(weekData, aes(x=firstPB, y=firstAverageQScore))+
  geom_point(data = subset(weekData, section == "random"),
             aes(colour=section, size=questionAttempt))+
  geom_point(data = subset(weekData, section != "random"),
             aes(colour=section), size = 5)+
  ...

這給了我這個,即使相關的questionAttempt值不同,您也可以看到 Mandatory 具有統一的大小:

在此處輸入圖像描述

也許更簡單的方法是在映射中定義值,這樣如果該部分不是“隨機”的,我們就會覆蓋傳入的值:

geom_point(aes(colour=section, 
           size=if_else(section=="random", questionAttempt, 50)))+

暫無
暫無

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

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