Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are these solid lines appearing below my plot in ggplot?

enter image description here

Here is my code:

pokemon <- read_csv("https://uwmadison.box.com/shared/static/hf5cmx3ew3ch0v6t0c2x56838er1lt2c.csv")

pokemon %>%  
  select(Name, type_1, Attack, Defense) %>% 
  mutate(AttDefRatio = Attack/Defense) %>% 
  ggplot(
    aes(Name, AttDefRatio)
  ) +
  geom_point(size = 0.5) +
  facet_wrap(type_1 ~ .) +
  theme(legend.position = "bottom")

for this home work assignment we are working with faceting, and everything pretty much looks how it should, but there those two thick lines at the bottom of the plot. If anyone has any idea why its happening that would be awesome!

like image 229
Michael Visconti Avatar asked May 31 '26 15:05

Michael Visconti


2 Answers

The two lines are actually the x axis labels, but you can remove them with axis.text.x=element_blank():

pokemon %>%  
    select(Name, type_1, Attack, Defense) %>% 
    mutate(AttDefRatio = Attack/Defense) %>% 
    ggplot(
        aes(Name, AttDefRatio)
    ) +
    geom_point(size = 0.5) +
    facet_wrap(type_1 ~ .) +
    theme(legend.position = "bottom", axis.text.x=element_blank())

enter image description here

Alternatively, if you want to show the individual Names by setting to free_x in facet_wrap, then change the text size in theme. Though it is a lot of information to show on one graph and will be difficult to see.

pokemon %>%  
  select(Name, type_1, Attack, Defense) %>% 
  mutate(AttDefRatio = Attack/Defense) %>% 
  ggplot(
    aes(Name, AttDefRatio)
  ) +
  geom_point(size = 0.5) +
  facet_wrap(type_1 ~ ., ncol = 5, scales = "free_x") +
  theme(legend.position = "bottom", axis.text.x=element_text(angle = 90, vjust = 0.5, hjust=1, size = 3))

enter image description here

like image 162
AndrewGB Avatar answered Jun 03 '26 04:06

AndrewGB


Those are your x axis labels. The labels are much longer than the available space after faceting, so they’re overlapping into a big jumble.

Possible solutions:

  • use coord_flip(), which is often a nice solution to long x axis labels
  • recode the x variable labels to be shorter
  • facet to fewer columns using the ncol argument to facet_wrap()
  • reduce the number of facets by collapsing or removing groups with few cases

I think your issue is compounded by every Pokémon name being repeated for each facet column, even though only a subset actually appears in each facet. You can fix this by setting scales = "free_x" within facet_wrap(), so that only labels included in each facet will be included.

like image 35
zephryl Avatar answered Jun 03 '26 03:06

zephryl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!