试图在ggplot 2中创建一个boxplot

时间:2017-10-03 02:06:40

标签: ggplot2 r-markdown

我非常陌生,请耐心等待我

我想要一个超简单的箱线图,X轴为0,10,20 ... 100,Y轴为um。这是我读过的excel数据。

percent | um
0 9
0 9
0 6
0 9
0 6
10 3
10 3
10 3
10 3
10 3
20 9
20 6
20 9
20 9
20 9
30 6
30 6
30 6
30 9
30 9
40 6
40 9
40 6
40 6
40 9
50 3
50 3
50 6
50 6
50 3
60 9
60 6
60 6
60 6
60 9
70 6
70 6
70 6
70 6
70 3
80 3
80 6
80 6
80 3
80 3
90 3
90 3
90 3
90 3
90 6
100 3
100 3
100 6
100 6
100 6

我尝试了一些不同的代码变体,目前它看起来像这样

ggplot(data, aes(y=data$um, x=data$percent)) + geom_boxplot () 

我收到了错误

Error: Aesthetics must be either length 1 or the same as the data (55): x, y.

我以前尝试将百分比设置为一个因子,值为0-100,增加10,因为它读为0但r总是告诉我标签11需要为1或55

1 个答案:

答案 0 :(得分:0)

尝试

df$percent <- as.factor(df$percent)
df$um <- as.numeric(df$um) 

# Then ggplot
ggplot(df, aes(x = percent, y = um)) + geom_boxplot()

由于存在的数据种类不多,您的箱图应该看起来很奇怪。

enter image description here

下次,请以更好的格式发布您的数据,以便其他人更快地帮助您解决问题。