【问题标题】:Shiny R: Plot another column of data on scatterplot using ggplot2Shiny R:使用 ggplot2 在散点图上绘制另一列数据
【发布时间】:2016-05-14 14:00:24
【问题描述】:

我目前有一个散点图,仅显示我的数据集中的一列,我是从 csv 读取的。我想同时绘制 ATI 和 BTI。

PP  BTI     ATI
1   9710    9660
2   10000   9900
3   10300   10100
4   10600   10400
.
.
.
99  159000  107000  

我的代码如下所示:

#server.R

#Income Percentile Scatterplot
incomedata <- read.csv("/Users/mathewsayer/Documents/Work/Level 7/Shiny Flat Tax/Flat Tax App/data/incomedist.csv")

ranges <- reactiveValues(x = NULL, y = NULL)

output$plot1 <- renderPlot({
  ggplot(incomedata, aes(x = BTI, y = PP)) + 
    geom_point() + 
    coord_cartesian(xlim = ranges$x, ylim = ranges$y)
})

#Brush and zoom on scatterplot
observeEvent(input$plot1_dblclick, {
  brush <- input$plot1_brush
  if (!is.null(brush)) {
    ranges$x <- c(brush$xmin, brush$xmax)
    ranges$y <- c(brush$ymin, brush$ymax)
  }
  else {
    ranges$x <- NULL
    ranges$y <- NULL
  }
})

我尝试像这样添加 ATI:aes(x = BTI:ATI, y = PP),但我收到错误消息 Aesthetics must be either length 1 or the same as the data (99): x, y

将我的数据称为框架或表格会更好吗?任何帮助将不胜感激。

编辑:黑色绘图点是 BTI,我希望来自 ATI 的数据看起来与我刚刚完成的这张照片模拟相似。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我不确定您为什么希望百分位数位于 y 轴上,但这里的代码可以满足您的需求:

    library(dplyr)
    library(shiny)
    library(tidyr)
    library(ggplot2)
    
    # simulate some data 
    df_foo = data_frame(
      percentile = seq.int(99),
      ATI = sort(rnorm(99)),
      BTI = sort(rnorm(99))
    )
    
    # UI
    ui_foo = shinyUI(
      plotOutput("plot_foo")
    )
    
    # server
    server_foo = shinyServer(function(input, output) {
      output$plot_foo = renderPlot({
        df_foo %>% 
        gather(key = var_name, value = value, -percentile) %>% 
        ggplot(aes(x = value, y = percentile, group = var_name, color = var_name)) + 
        geom_line() + 
        theme_bw()
      })
    })
    
    # run the server
    shinyApp(ui = ui_foo, server = server_foo)
    

    您的问题从根本上讲是关于如何在 ggplot2 中绘制多个变量,并且您需要在适当重塑的数据中基本上指定 group 美学。

    【讨论】:

    • 我将百分位放在 y 轴上的原因是用户知道他们的收入,但不知道他们所处的百分位。因此他们会首先阅读 x 轴以找到自己的位置之前抬头看看有什么百分位。当然,我即将使用的 second question 将使这个半冗余。
    【解决方案2】:

    为了同时绘制BTIATI,您需要定义哪一个绘制在哪个轴上。换句话说,您应该告诉ggplot,您希望绘制两列,这在您的代码中缺失,即在ggplot(incomedata, aes(x = BTI, y = PP)) 中。在这段代码中,您要求将 BTI 绘制在 x 轴上,将 PP 绘制在 y 轴上,据我了解,您根本不希望绘制 PP。

    您能否尝试下面的代码,看看它是否符合您的预期?

    ggplot(incomedata, aes(x = BTI, y = ATI)) + 
        geom_point() + 
        coord_cartesian(xlim = ranges$x, ylim = ranges$y)
    

    【讨论】:

    • 感谢您的回复。回想起来,我似乎需要将 x 轴作为“收入”之类的东西。 PP 在 y 轴上是必不可少的。我当前的代码按照应有的方式绘制了 BTI,我在上面添加了一张图片,可以阐明我对 ATI 的目标。
    • 您可以轻松地将reshape 数据从长格式转换为宽格式,然后您将所有 ATI 和 BTI 值放在一个列中,另一个新列将定义Category每个值都属于(ATI 或 BTI)。然后从那里您可以轻松地在 X 轴上绘制 Values 列,在 y 轴上绘制 PP 值,然后您应该添加属性 colour=Category 以便根据它们所属的类别(ATI 或 BTI)对值进行着色。看到这个:stackoverflow.com/questions/33101923/…
    【解决方案3】:

    感谢您的帮助 bert 和 tchakravarty。

    我将我的数据重新调整为长格式,并且效果很好。

    所以我做的是:

    • 按照@Bert 建议的新类别列将我的数据重新整形为长格式

    • 将我的 ggplot() 更改为:

    output$plot1 <- renderPlot({ ggplot(incomedata, aes(x = Income, y = PP)) + geom_point(aes(colour=Category, group=Category)) + coord_cartesian(xlim = ranges$x, ylim = ranges$y) })

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      相关资源
      最近更新 更多