Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 204922

How to make my graph legend not a gradient? ggplot2, tidyr, poisson regression, binary and logistic predictor

$
0
0

This is kind of a really small issue, but I can't rectify it without causing unwanted ramifications... unless someone can help.

I'm doing a poisson regression and I have 7 predictor variables, 3 are continuous and 4 are binary, being the results are 0s and 1s, but not logistic: not true or false.

I'm plotting a graph of my model using ggplot2 and geom_scatter. I'm doing a continuous ~ predictions (continuous), but setting the colour of the lines to gender, which is binary 0 and 1s. My graph works fine except the legend shows a gradient from 0 to 1. I would just like it to be 0 and 1, or male and female or something.

I've recoded the var gender as logistic, and this mostly works out; although i get a true/false in the legend, but no gradient. However it fails to work when I try and knit to PDF and it is upset.

It says, for line 366 which is the expand_grid im creating: variable 'female' (which is my gender var) was fitted with type "numeric" but type "logical" was supplied.

Yet even when I view my data frame it clearly shows female as logical, and even puts instead of .

I'm sure there are multiple way to resolve this, i'd appreciate some advice. Am i just using expand_grid incorrectly? Although I can't see anything about logistic columns in the help section for expand_grid. Or can I leave gender as 0s and 1s then adjust the graph to remove the gradient? if so how?

Thanks, Tom

doctors_tibble <- expand_grid(female = c(TRUE, FALSE),
                              age = seq(25, 65, by=10),
                              outwork = 0,
                              kids = 1,
                              hhninc = median(doctors$hhninc),
                              educ = median(doctors$educ),
                              self = 1)
#is what it looks like when it is logistic

doctors_tibble <- expand_grid(female = c(0, 1),
                              age = seq(25, 65, by=10),
                              outwork = 0,
                              kids = 1,
                              hhninc = median(doctors$hhninc),
                              educ = median(doctors$educ),
                              self = 1)
#is what it looks like originally, 0 and 1s. 

#here is a bit more code in case it helps

doctors$female = as.logical(doctors$female)      #me turning gender var logical        

vis_preds <- add_predictions(doctors_tibble, docm3, type = 'response') #making predictions


ggplot(vis_preds,
       aes(x = age, y = pred, colour = female)           #plotting my graph
) + geom_point() + stat_smooth(method = lm, se = F) +
  theme_minimal()



Viewing all articles
Browse latest Browse all 204922

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>