【问题标题】:plotting boolean variable as geom_line将布尔变量绘制为 geom_line
【发布时间】:2021-05-21 23:28:50
【问题描述】:

我对 R 和 ggplotting 还是很陌生。我正在尝试将 TRUE 观察值随时间的总数绘制成线图,但计数似乎上限为 1.00。

dd <- data.frame(x = c(1,1,1,2,2,3,3), 
                 y = c(TRUE,TRUE,TRUE,FALSE,TRUE,TRUE,TRUE))

ggplot(dd,aes(x,as.numeric(y)))+
  geom_line()

count(as.numeric(y)) 不起作用,你能帮帮我吗?

【问题讨论】:

  • 试试ggplot(count(dd, x, wt = y), aes(x, n))+ geom_line()

标签: r ggplot2 boolean


【解决方案1】:

建议在绘制图表之前准备好统计数据

library(ggplot2)
library(dplyr)

# summarized the TRUE count for each x value
graph_data <- dd %>%
  group_by(x) %>%
  summarise(count_y = sum(y))

# plot the data using geom_line
ggplot(data = graph_data) +
  geom_line(aes(x, count_y)) +
  # added the scale y for start the y-axis from 0
  scale_y_continuous(limits = c(0, NA), expand = c(0, 0))

reprex package (v2.0.0) 于 2021 年 5 月 22 日创建

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 2016-05-09
    • 2016-09-19
    相关资源
    最近更新 更多