我目前正在尝试做一个线形图,代表3年来各种类型手术的医生数量的演变(每一行),我的数据框如下:
type of operation number of doctors year
ambulatoire 12 2019
externe 150 2019
ambulatoire 19 2020
externe 3 2020
我试过下面的代码,但它似乎不工作…有谁能帮忙吗?
ggplot(df) +
geom_line(aes(x = df$year, y = df$"number of doctors", color = df$"type of operation"))
试试这个
ggplot(df) +
geom_line(aes(x = year,
y = `number of doctors`,
color = `type of operation`))
试试这个
library(ggplot2)
df <-
data.frame(
type_of_operation = c("ambulatoire", "externe"),
number_of_doctors = c(12, 150, 19, 3),
year = c(2019, 2019, 2020, 2020)
)
ggplot(df, aes(x = year, y = number_of_doctors, color=type_of_operation)) +
geom_line()
PS:如果你想在变量名中保留空格,请使用引号:'而不是">