ggplot2拒绝绘制数值

问题描述 投票:0回答:2

我目前正在尝试为我的大学项目创建一个简单的箱线图,似乎无法弄清楚我做错了什么。

我当前的代码如下:

ggplot(wait_c_long, aes(x='Period', y='Days waited at the 50th percentile')) + geom_point()
wait_c_long = My dataframe

Period = Ordered factor class variable of time periods e.g. '2014-15'...'2018-19'.

第50个百分位点的等待天数=平均等待时间的数值类变量。

以下是我的数据集的屏幕截图,仅供参考:Dataset Image每当我运行上述命令时,它都会导致以下输出Output仅具有单个数据点,并告诉我y轴上的变量是离散的,尽管它是数字的

[理想情况下,我想使用geom_point函数使用以下aes(x='Period',y='Days waited at the 50th percentile', color = 'State', size = 'Admissions')创建一个简单的散点图,以便从我的数据集中创建数据可视化,可以在分配中使用它,因此我们将不胜感激。

r ggplot2 boxplot
2个回答
0
投票

通常在aes()中,您不会引用数据框列的名称,但是,当它们有空格时,这是行不通的,因此您需要使用开放的单引号引起来。您最终得到以下代码:

library(ggplot2)

#Create an example dataset in the same format
wait_c_long <- data.frame(Period=1:5, Days=1:5)
names(wait_c_long)[2] <- 'Days waited at the 50th percentile'

#Use no quotes/forward quotes to reference the columns
ggplot(wait_c_long, aes(x=Period, y=`Days waited at the 50th percentile`)) + geom_point()

enter image description here


0
投票

尝试为变量使用反引号代替撇号。

ggplot(wait_c_long, aes(x=`Period`, y=`Days waited at the 50th percentile`)) +
    geom_point()

wait_c_long = My dataframe

Period = Ordered factor class variable of time periods e.g. '2014-15'...'2018-19'.
© www.soinside.com 2019 - 2024. All rights reserved.