0

See the follow plot for example:

library(ggplot2)

df <- head(mtcars, 4)

df$rn <- rownames(df)

#>                 mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4      21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag  21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710     22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#>                            rn
#> Mazda RX4           Mazda RX4
#> Mazda RX4 Wag   Mazda RX4 Wag
#> Datsun 710         Datsun 710
#> Hornet 4 Drive Hornet 4 Drive

ggplot(df, aes(x = rn, y = mpg)) +
    geom_point() +
    scale_x_discrete(expand = c(0, 0))

enter image description here

I want to remove the extra region in the grey plot area with expand = c(0, 0), while avoiding the last x axis label 'Mazda RX4 Wag' from being cropped. How can I achieve this?

mt1022
  • 16,834
  • 5
  • 48
  • 71
  • @beetroot, thanks for the link. It is really what I want. I'll delete this post. – mt1022 Apr 25 '17 at 12:01
  • @Axeman, Is it good to leave a duplicated question here? I am really not sure which way is more appropriate. – mt1022 Apr 25 '17 at 12:04
  • Well I found the answer from here: https://meta.stackoverflow.com/questions/265736/should-i-delete-my-question-if-it-is-marked-as-a-duplicate – mt1022 Apr 25 '17 at 12:10
  • Yes you can leave duplicated questions since they serve as sign posts for the target post. – Axeman Apr 25 '17 at 12:15

1 Answers1

0

What about this?

ggplot(df, aes(x = rn, y = mpg)) +
  geom_point() +
  scale_x_discrete(expand = c(0, 0))+
  coord_fixed(ratio = 1/0.40)
Janis S.
  • 37
  • 4
  • Thanks. `coord_fixed` is a new function for me. But I prefer the solution to the linked question, which is more flexible. – mt1022 Apr 25 '17 at 12:06