Issue with creating a Histogram plot in R

25 Views Asked by At

I'm not sure what I'm doing wrong but this is what the error is:

Error in fortify(): ! data must be a <data.frame>, or an object coercible by fortify(), not a object. ℹ Did you accidentally pass aes() to the data argument? Run rlang::last_trace() to see where the error occurred.

q1 <- Hsall_df %>% filter(code == 118)

ggplot(aes(x=party_label, y=nominate_dim1))+

  • geom_histogram()+
  • labs(x="Party", y= "First Dimension Esitmate")+
  • theme_bw()

Error in fortify(): ! data must be a <data.frame>, or an object coercible by fortify(), not a object. ℹ Did you accidentally pass aes() to the data argument? Run rlang::last_trace() to see where the error occurred.

1

There are 1 best solutions below

0
jeffreyohene On

without any sample data to reproduce this, from your code it looks like you did not pipe the data before your ggplot call that is why your error says data must be data.frame. try this code below which includes the pipe operator like @VinceGreg suggested:

q1 <- Hsall_df %>% filter(code == 118) %>%
  ggplot(aes(x=party_label, y=nominate_dim1)) +
  geom_col() +
  labs(
    x="Party",
    y= "First Dimension Esitmate") +
  theme_bw()

q1