1

I currently have a scatterplot showing only one column from my data set, which I'm reading from a csv. I want to plot both ATI and BTI.

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

My code looks like this:

#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
  }
})

I've tried adding ATI like this: aes(x = BTI:ATI, y = PP) but I get the error message Aesthetics must be either length 1 or the same as the data (99): x, y

Would I be better calling my data as a frame or table? Any help would be greatly appreciated.

EDIT: The black plot points are BTI, I want to the data from ATI to appear similar to this photo-mock up I just done. example of how it should look when done

3 Answers 3

1

I am not sure why you want the percentiles to be on the y-axis, but here is code that does what you want:

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)

Your question is more fundamentally about how to plot multiple variables in ggplot2 and you need to basically specify the group aesthetic in suitably reshaped data.

Sign up to request clarification or add additional context in comments.

1 Comment

My reasoning behind having the percentile on the y-axis is that users know their income, but not what percentile they're in. Therefore they'd read across the x-axis first to find their place there before looking up to see what percentile. Of course my second question which I'm just about to move onto will make this semi-redundant.
0

In order to plot both BTI and ATI, you need to define which one is plotted on which axis. In other words, you should tell ggplot that you would like both columns plotted, this is missing in your code, i.e. in ggplot(incomedata, aes(x = BTI, y = PP)). In this code you are asking BTI to be plotted on x axis and PP on y axis, and as far as I understand you do not want PP to be plotted at all.

Can you please try the code below and see if it does what you are expecting?

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

2 Comments

Thanks for your reply. In retrospect it seems I need to have the x axis as something like 'Income'. PP is essential on the y axis. My current code plots BTI as it should be, I've added an image above that should clarify what I'm aiming for with ATI.
You can easily reshape your data from long format to wide format, then you will have all your ATI and BTI values in one column all together, and another new column will define to which Category each value belongs to (to ATI or BTI). Then from there you can easily plot the Values column on X axis, and PP values on y axis and then you should add attribute colour=Category so that the values are coloured based on the category (ATI or BTI) they belong to. See this: stackoverflow.com/questions/33101923/…
0

Thanks for your help bert and tchakravarty.

I reshaped my data into long format and it worked perfectly.

So what I did was:

  • Reshape my data into long format as @Bert suggested with a new Category column

  • Changed my ggplot() to this:

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) })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.