1

I am looking for a way to help better visualize the relationship between a independent continuous variable and a binary response variable.

I am trying to understand how I can add a 2nd y axis to the existing plot I have below. I want to get a sense of the response rate over different numerical ranges visually.

  1. How can I add in the response percent at any given histogram bin? For example if there were 10 observations in a bin and 2 were the positive class, then this would show a response of 20%.
  2. Ideally it's possible that this would be dynamic in that I might change the # of bins. For instance, I have 10 here, I might want 20 the next time.
  3. This would be a connected line-chart with the corresponding percentages from #1 on the right y axis.

Or in other words, I want a line chart of the positive class to be displayed as a line chart with % show in Y axis.

library(mlbench)
library(tidyverse)
data(Sonar)   ## from mlbench
library(ggplot2)

ggplot(Sonar, aes(x=V11, fill=Class)) +
  geom_histogram(col='black', bins = 10) +
  scale_fill_manual(values=c("purple", "green"))  +
  labs(title = "Count Left Y Axis; 'R' class percent of BIN in Right Y Axis" ,
       x = 'Variable Value in this case V33', y ='Count of Observations' )

1 Answer 1

2

Not sure if this is what you are after but the description you gave sounded very similar to a conditional density plot.

ggplot probably has an alternative to this, but with base R:

cdplot(Class ~ V1, Sonar, col=c("cornflowerblue", "orange"), main="Conditional density plot")

And the result:

cdplot

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

1 Comment

Yes, sorry ultimately I'm trying to plot continuous vs a categorical (binary) response. I'd like to see the percent positive of this response over various numerical ranges. This is in-line with that idea and helpful, but I'd like to be able to see the actual percentage of 'R' at any given numerical range of V1 in a cleaner way. It's also helpful to have some sense of the distribution of V1 or the frequency of V1 which this plots masks.

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.