使用read.table中的数据使用ggplot2进行方框图
我正在绘制一个显示学生身高的方框图。然而,我不确定我用什么作为x和y。我只有测量,所以一个应该是身高,另一个应该是有那个身高的学生数量使用read.table中的数据使用ggplot2进行方框图,r,R,我正在绘制一个显示学生身高的方框图。然而,我不确定我用什么作为x和y。我只有测量,所以一个应该是身高,另一个应该是有那个身高的学生数量 x=N, y=Height 我的代码: # Library library(ggplot2) library(tidyverse) # 1. Read data (comma separated) data = read.table(text = "184,180,183,184,184,160,173",
x=N, y=Height
我的代码:
# Library
library(ggplot2)
library(tidyverse)
# 1. Read data (comma separated)
data = read.table(text = "184,180,183,184,184,160,173",
sep=",",stringsAsFactors=F, na.strings="unknown")
# 2. Print table
print(data)
# 3. Plot box plot
data %>%
pivot_longer(cols = everything()) %>%
ggplot(aes(x=value, y=value)) +
geom_boxplot() +
theme_classic() +
xlab("Students") +
ylab("Height") +
ggtitle("Height of students")
要正确使用箱线图,必须有一个分类变量和一个连续变量。将分类(如make、female等)放在x轴上,将连续放在y轴上(在您的情况下是高度)。我认为表示数据向量的最佳绘图是直方图。但是,您可以通过创建虚拟因子来使用箱线图对观察结果进行分组。i、 e
data %>%
pivot_longer(cols = everything()) %>%
mutate(type="student") %>%
ggplot(aes(x=type, y=value)) +
geom_boxplot() +
theme_classic() +
xlab("Students") +
ylab("Height") +
ggtitle("Height of students")
如果你想要一个柱状图(我认为更适合你的情况),你不需要虚拟因子,你可以这样做:
data %>%
pivot_longer(cols = everything()) %>%
ggplot(aes(x=value)) +
geom_histogram() +
theme_classic() +
xlab("Students") +
ylab("Height") +
ggtitle("Height of students")
x不能是具有该高度的学生数量吗?通常情况下,您会给出
ggplot
未聚合的数据。感谢您的帮助和输入。我需要方块图。我已经有柱状图了。