簡體   English   中英

如何自定義我的 x 軸以反映真實的參與者人數?

[英]How do I customise my x-axis to reflect the true number of participants?

因此,我正在嘗試創建一個折線圖,顯示許多參與者干預前后的分數。 但是,參與者的數量並不反映 x 軸的比例。 例如(見圖)x 軸從 2 變為 7。但是,我希望 x 軸只顯示完成問卷的參與者。 例如 2、3、5、7。 有誰知道如何做到這一點? 我的代碼如下: enter image description here

ggplot(data = my_data, aes(x = Participant)) +
  geom_line(aes(y = PRE_QUIP_RS, colour = "PRE QUIP RS")) +
  geom_point(aes(y = PRE_QUIP_RS, colour = "PRE QUIP RS")) +
  geom_line(aes(y = POST_QUIP_RS, colour = "POST QUIP RS")) +
  geom_point(aes(y = POST_QUIP_RS, colour = "POST QUIP RS")) +
  scale_colour_manual("", 
                      breaks = c("PRE QUIP RS", "POST QUIP RS"),
                      values = c("blue", "orange")) +
  xlab("Participants ") +
  scale_y_continuous("QUIP RS Scores", limits = c(0,30)) + 
  labs(title="Pre and Post QUIP RS Scores")

在此處輸入圖像描述

強迫Participant考慮因素。 因子在內部編碼為連續整數,因此主要問題已解決。

至於代碼重復到plot這幾列,這種問題一般是數據重新格式化的問題。 請參閱將 data.frame 從寬格式重塑為長格式

我稍微簡化了列名。

suppressPackageStartupMessages({
  library(ggplot2)
  library(dplyr)
  library(tidyr)
})

set.seed(2022)
Participant <- c(2,3,5,7)
PRE <- sample(20:30, 4)
POST <- sample(10:20, 4)
my_data <- data.frame(Participant, PRE, POST)

my_data %>%
  pivot_longer(-Participant) %>%
  mutate(Participant = factor(Participant)) %>%
  ggplot(aes(x = Participant, value, color = name, group = name)) +
  geom_line() +
  geom_point() +
  scale_colour_manual("", 
                      breaks = c("PRE", "POST"),
                      values = c("blue", "orange")) +
  xlab("Participants") +
  scale_y_continuous("QUIP RS Scores", limits = c(0,30)) + 
  labs(title="Pre and Post QUIP RS Scores")

創建於 2022-09-29,使用reprex v2.0.2

暫無
暫無

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

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