I am trying to plot a regression, but I keep getting a bar grpag

Hi, I am doing a political science project on voting behavior and trying to make a regression.

Model_2<-glm(gop_positive~dem_angry, data= my_data_set, family="binomial") 
summary(Model_2)

plot(gop_positive, dem_angry, main = "Model Graphed",
     xlab = "Is There Anything the Respondent Likes About Trump?", 
     ylab = "How Often does Respondent Feel Angry Towards Clinton?",
     pch = 19)

I do this and get a bar graph that is accurate and I'm going to use it, but i need to get a regression and it's not doing that.

With only two variables, both binary, an correlation analysis is more appropriate. The logistic regression model being used yields a log likelihood of observing gop_positive in the presence of dem_negative and its plot presentation is quite different from ordinary least squares regression. I have an explainer here.

1 Like

Your plot command just does a scatter plot. It doesn't know anything about the regression you've run.


This is what it looks like.

I've attached a photo and it does not come out as a scatter plot. It shows relevant things but for my project, i need regressional analysis.

I admit I'm not sure why you're getting a bar graph instead of a scatter plot. But the main thing is that you plot command doesn't reference the regression Model_2.

You might want to post your data with dput() if it isn't too large.

I've started getting the chart for a scatterplot, when use

ggplot(data = my_data_set, aes('gop_positive','dem_angry')) +
geom_point() +
geom_smooth(method=lm, se= FALSE)

But i don't get all the data points showing up as a scatter plot

This is what I get in the console

ggplot(data = my_data_set, aes('gop_positive','dem_angry')) +

  • geom_point() +
  • geom_smooth(method=lm, se= FALSE)
    geom_smooth() using formula = 'y ~ x'

Post your data, so that someone might spot the problem.

1 Like

you are quoting aesthetics; which is to say; not referring to the columns in your dataset but to the literal strings you are putting; you can either use aes_string() instead of aes() , or remove the quote marks, or replace the quote marks with backticks

1 Like

A cor() will illuminate.

Like the whole data set?

I changed it to this:
ggplot(data= my_data_set, aes_string(gop_positive,dem_angry)) +
geom_point() +
geom_smooth(formula= y~x) +
theme_minimal()+
labs(x="Is There Anything the Respondent Likes About Trump?",
y= "How Often does Respondent Feel Angry Towards Clinton?",
title= "Relationship A") +
theme(plot.title = element_text(hjust=0.5, size=15, face = 'bold'))

and got this
Error in geom_point():
! Problem while computing aesthetics.
:information_source: Error occurred in the 1st layer.
Caused by error in check_aesthetics():
! Aesthetics must be either length 1 or the same as the data (10)
:heavy_multiplication_x: Fix the following mappings: x and y
Run rlang::last_error() to see where the error occurred.

Youve switched to aes_string but dropped the quotes that made them strings...

I put the quotes
Error in geom_point():
! Problem while computing aesthetics.
:information_source: Error occurred in the 1st layer.
Caused by error in check_aesthetics():
! Aesthetics must be either length 1 or the same as the data (10)
:heavy_multiplication_x: Fix the following mappings: x and y
Run rlang::last_error() to see where the error occurred.

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

2 Likes

It would be good to follow @nirgrahamuk's (always) helpful advice about a reprex, etc. Remember that you can see all kinds of things that we can't see.

If the data set is large, then just post part of it.

1 Like

It's election data I downloaded from the website.

Its up to you to decide to what degree you want to cooperate with those making suggestions in order to help you.

I think this is correct. I apologize, I'm new to these forums an learning how they work.

head(my_data_set)
gender x1 x2 x3
1 2 10 54 75
2 2 2 35 55
3 1 12 94 63
4 1 6 79 56
5 1 7 24 59
6 2 4 87 54

This is a poor approach as it does not indicate the variable types in a rigourous way. dput() is the recommended approach. The guide covers this.

2 Likes