【问题标题】:How to create an Unstacked Dot Plot in RStudio? [closed]如何在 RStudio 中创建未堆叠的点图? [关闭]
【发布时间】:2020-10-21 21:10:52
【问题描述】:

如何在 RStudio 中创建此点图

Unstacked Dot Plot of interest-rates of 50 loans

【问题讨论】:

    标签: r ggplot2 plot graph


    【解决方案1】:

    这样的?

    library(tidyverse)
    data.frame("Interest_Rate" = runif(n = 40, min = 0, max = 0.25),
               "Interest_Rate_y" = rep(1.1, 40),
               "Mean_Value_y" = rep(0.21, 40)) %>%
      ggplot() +
      geom_point(aes(y = Interest_Rate_y, x = Interest_Rate),
                 size = 10, col = "deepskyblue", alpha = 0.25) +
      geom_point(aes(y = Mean_Value_y, x = mean(Interest_Rate)),
                 size = 8, pch = 17, col = "red") +
      geom_hline(yintercept = 0.75) +
      coord_cartesian(ylim = c(0,1.5), clip = "off") +
      scale_x_continuous(name = "Interest Rate", labels = scales::percent) +
      theme_classic(base_size = 16) +
      theme(axis.title.y = element_blank(),
            axis.text.y = element_blank(),
            panel.grid = element_blank(),
            axis.line.y = element_blank(),
            axis.ticks.y = element_blank())
    

    【讨论】:

    • 完美。 (1) 但是,我看到您在图表上基本上创建了 3 个不同的对象:一个点图、一个 y 截距和一个红色三角形。难道没有别的办法了吗? (2) 另外,我对您如何创建三角形有点困惑。在它的美学参数中,我可以看到 x 只是 1 个值(平均值),但 y 是一个有 50 个值的列。两个美学参数中的值的数量不应该相同吗?
    【解决方案2】:

    首先我们需要一些数据。以下与图片中的数据大致匹配:

    set.seed(69)
    my_data <- rgamma(40, 5, 40)
    

    我们可以像这样使用ggplot2 绘制它:

    library(ggplot2)
    
    ggplot(data.frame(x = my_data, y = 1), aes(x, y)) +
      geom_point(size = 5, color = "#579fc4", alpha = 0.5) +
      scale_y_continuous(limits = c(0.5, 1.5), breaks = c(0, 2)) +
      scale_x_continuous(labels = scales::percent_format(1), 
                         breaks = c(0.05, 0.1, 0.15, 0.2, 0.25),
                         name = "Interest rate") +
      theme_minimal() +
      theme(axis.line.x = element_line())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      • 2012-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多