![](/img/trans.png)
[英]ggplot2 R fix x-axis label at a specific point relative to plot
[英]How to plot multiple 1 dimensional scatter plot in R based on occurences of a specific value at a specific point of x-axis
我有这些数据来确定缺勤率如何影响学生在 3 个不同年份的成绩,我正在尝试 plot 一个一维散点图 plot,其中三个不同年份在 y 轴 G1、G2 和 G3 中,而缺勤是 x 轴。 一维散点 plot 应该出现 plot 次 grade = 0 在特定的缺失值中,如下图所示。
我的数据:
structure(list(absences = c("6", "4", "10", "2", "4", "10", "0",
"6", "o", "0", "0", "4", "2", "2", "0", "4", "6", "4", "16",
"4"), G1 = c(5, 5, 7, 15, 6, 15, 12, 6, 16, 14, 10, 10, 14, 10,
14, 14, 13, 8, 6, 8), G2 = c(6, 5, 8, 14, 10, 15, 12, 5, 18,
15, 8, 12, 14, 10, 16, 14, 14, 10, 5, 10), G3 = c(6, 6, 10, 15,
10, 15, 11, 6, 19, 15, 9, 12, 14, 11, 16, 14, 14, 10, 5, 10)), row.names = c(NA,
-20L), spec = structure(list(cols = list(absences = structure(list(), class = c("collector_character",
"collector")), G1 = structure(list(), class = c("collector_double",
"collector")), G2 = structure(list(), class = c("collector_double",
"collector")), G3 = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), delim = "\t"), class = "col_spec"), problems = <pointer: 0x55e465b58110>, class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))
是这样的吗?
library(dplyr)
library(tidyr)
library(ggplot2)
set.seed(2022)
tibble(
absences = sample(c(0:16), 20, replace = TRUE),
G1 = sample(c(0:16), 20, replace = TRUE),
G2 = sample(c(0:16), 20, replace = TRUE),
G3 = sample(c(0:16), 20, replace = TRUE)
) %>%
pivot_longer(
cols = -absences,
names_to = "key",
values_to = "value"
) %>%
filter(key != 0) %>%
ggplot(aes(absences, key)) +
#geom_jitter(color = "red", height = 0.1)
geom_point(color = "red")
我不知道你的成绩和缺勤是否不同。 如果没有,你可以使用我引用的geom_jitter()
。
Output:
这是您开始完成任务的一种方式:
library(tidyverse)
df %>%
pivot_longer(
-absences
) %>%
mutate(absences = as.numeric(replace(absences, absences == "o", "0"))) %>%
group_by(absences, name, value) %>%
summarise(absences = sum(absences, na.rm = TRUE)) %>%
ggplot(aes(x=name, y=factor(absences)))+
geom_point(aes(size = value), color="red")+
theme_minimal() +
labs(title = "Your title", y ="Absences", x = "Year") +
theme(legend.position = "bottom",
plot.title = element_text(hjust = 0.5)) +
guides(color= guide_legend(), size=guide_legend())+
coord_flip()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.