0

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

5
  • batter_last_name is a factor, not a continuous variable. Hence the error. Also, I strongly usrge you not to use limits to define the limits of your axes. This filters the data and then constructs summaries, which can lead to unexpected and erroneous results. coord_cartesian is far better, IMHO, because it uses the whole dataset to construct the graph and then "zooms" to the required region. Commented Oct 24, 2022 at 12:49
  • See, for example here Commented Oct 24, 2022 at 12:51
  • This didn't work. It put the batter_name along the X and the distance along the Y even though x = distance and y = batter_name. I did this before without any issues when ranking album sales. Album was a character though but that also didn't work here. Commented Oct 24, 2022 at 14:31
  • Then we need a minimal reproducible example. This post will help you construct one. Commented Oct 24, 2022 at 14:33
  • coord_flip() flips the axes. If you don't want the axes flipped, don't use coord_flip. Commented Oct 24, 2022 at 16:05

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The first code solution you shared yielded the same results. However, the 2nd code solution works. This is very odd to me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.