Home > Enterprise >  Segment/Point ggplot2 is reversing x & y axis
Segment/Point ggplot2 is reversing x & y axis

Time:10-25

I want to create a segment/point plot that has the topBatter on the Y axis and the topTen Distance on the Y axis. When I create it the plot reverses the X and Y.

I'm a bit stuck as to why this one isn't working.

Thanks for any guidance. I appreciate this community.

#Top HR by Distance
topTenDistance <- c(485, 502, 484, 492, 489, 499, 500, 497, 489, 501)
topBatter <- c("Ba", "Ca", "Da", "Fa", "Ga", "Ha", "Ja", "Ka", "Ya", "Ja")
topCombined <- cbind(topBatter, topTenDistance)
topCombined <- as.data.frame(topCombined)

topCombined %>%
  ggplot(aes(x=topTenDistance, y=topBatter))  
  geom_segment( aes(xend=topTenDistance, yend=0))  
  geom_point( size=1.5, color="#134A8E")  
  scale_y_discrete()  
  coord_flip()  
  ggtitle("Top HRs by Distance")  
  labs(subtitle = "MLB 2022 Season")  
  xlab("Player's Name")  
  ylab("Distance (in ft")     

enter image description here

CodePudding user response:

A couple of things. First, after

topTenDistance <- c(485, 502, 484, 492, 489, 499, 500, 497, 489, 501)
topBatter <- c("Ba", "Ca", "Da", "Fa", "Ga", "Ha", "Ja", "Ka", "Ya", "Ja")
topCombined <- cbind(topBatter, topTenDistance)
topCombined <- as.data.frame(topCombined)

topCombined$topTenDistance is character:

as_tibble(topCombined)
# A tibble: 10 × 2
   topBatter topTenDistance
   <chr>     <chr>         
 1 Ba        485           
 2 Ca        502           
 3 Da        484           
 4 Fa        492           
 5 Ga        489           
 6 Ha        499           
 7 Ja        500           
 8 Ka        497           
 9 Ya        489           
10 Ja        501           

That's not helping. So construct your data.frame like this:

topCombined <- data.frame(
                 topTenDistance=c(485, 502, 484, 492, 489, 499, 500, 497, 489, 501),
                 opBatter=c("Ba", "Ca", "Da", "Fa", "Ga", "Ha", "Ja", "Ka", "Ya", "Ja")
               )

Then

topCombined %>%
  ggplot(aes(x=topTenDistance, y=topBatter))  
  geom_segment(aes(xend=480, yend=topBatter))  
  geom_point( size=1.5, color="#134A8E")  
  scale_y_discrete()  
  ggtitle("Top HRs by Distance")  
  labs(subtitle = "MLB 2022 Season")  
  ylab("Player's Name")  
  xlab("Distance (in ft)") 

produces

enter image description here

Is this what you want? You can set the value of xend in the call to geom_segment to be whatever you find aesthetically pleasing.

Note that the "double dot" for Ja is a feature of your test data.

Incidentally,

topCombined %>%
  ggplot(aes(y=topBatter))  
    geom_linerange(aes(xmin=480, xmax=topTenDistance))  
    geom_point(aes(x=topTenDistance), size=1.5, color="#134A8E")  
    labs(
      title="Top HRs by Distance",
      subtitle = "MLB 2022 Season",
      y="Player's Name",
      x="Distance (in ft)"
    ) 

produces the same graph with a slightly simplified pipe.

  • Related