【问题标题】:Bar Graph, graphing Integers as strings条形图,将整数绘制为字符串
【发布时间】:2022-11-11 23:25:32
【问题描述】:

“性别”存储为整数:1 和 0 我正在尝试在条形图中将其绘制为 X 轴上的“男性”和“女性” 与赌徒的投注总数相比。

我认为有一种更简单的方法可以做到这一点,而不是将数据集中的每个输入都更改为字符串。

我不是试图给男性一个蓝色和女性紫色或黄色。

谢谢大家

代码:

# scatter plot of age of Gamblers correlated with number of bets 
#alpha keeps from over plotting remove if unnecessary
p_1 <- ggplot(data = data, aes(x = Gender, y = BetsA )) +
    geom_point(alpha = 0.1)
p_1 + ggtitle(label = "Gender Correlated with Total Number of Bets") + # for the main title
xlab(label = "Gender of Gambler") + # for the x axis label
ylab(label = "Total Number of Bets" ) # for the y axis label

【问题讨论】:

    标签: r ggplot2 visualization data-analysis


    【解决方案1】:

    一种选择是首先将您的Gender 列转换为factor。之后,您可以使用 scale_x_discretelabels 参数为 0 和 1 分配所需的标签。对于着色,您基本上可以这样做。只需将factor(Gender) 映射到color aes,然后通过scale_color_manualvalues 参数设置您想要的颜色:

    使用一些假的随机示例数据:

    set.seed(123)
    
    # Create example data
    data <- data.frame(
      Gender = rep(c(0,1), 100),
      BetsA = runif(200, 0, 40000)
    )
    
    library(ggplot2)
    
    ggplot(data = data, aes(x = factor(Gender), y = BetsA, color = factor(Gender) )) +
      geom_point(alpha = 0.1) +
      scale_x_discrete(labels = c("0" = "Male", "1" = "Female")) +
      scale_color_manual(values = c("0" = "blue", "1" = "purple"), labels = c("0" = "Male", "1" = "Female")) +
      ggtitle(label = "Gender Correlated with Total Number of Bets") +
      xlab(label = "Gender of Gambler") +
      ylab(label = "Total Number of Bets" )
    

    【讨论】:

    • 嗯,有没有办法让点变大,在 Y 轴上添加更多数字,让男性的列和感觉思想家,或者更好的图表?也许我应该做一个饼图?您认为什么对数据分析更好,这似乎有缺陷且混乱。
    • (:您可以通过size 增加点的大小,例如geom_point(alpha = 0.1, size = XXXX)。要获得更多数字(我猜是轴文本标签?)您可以使用scale_y_continuousbreaks 参数,例如scale_y_continuous(breaks = seq(0, 40000, by = 5000))。关于最好的可视化,我建议从箱线图开始。或者并排显示箱线图和单个数据点。参见例如这篇博文:cedricscherer.com/2021/06/06/…
    猜你喜欢
    • 2012-02-24
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    相关资源
    最近更新 更多