1

I have asked a question earlier.Here is the link:how to add a vertical line using theme() function in my plot

And now new problem happened,the horizontal line of the band6 can not display completely.Anyone can give me some suggestions?Thank you.

And my code is below:

p <- ggplot(data = df1, aes(x = df1$MeanDecreaseAccuaracy, y = reorder(factor(df1$Variables),df1$MeanDecreaseAccuaracy)))

p + geom_segment(aes(yend = df1$Variables,xend = 0)) + 
geom_point() + 
theme_minimal() + 
scale_x_continuous(expand = c(0,0),breaks = c(5,10,15,20,25,30,35,40,45)) + 
labs(x = "Mean Decrease in Accuracy",y = "Prdictors variable") + 
theme(axis.line = element_line(colour = "black"),
    axis.text.x = element_text(colour = "black"),
    axis.text.y = element_text(colour = "black"),
    axis.ticks.x = element_line(size = 0.2,colour = "black"),
    axis.ticks.y = element_line(size = 0.2,colour = "black"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank())

And the output figure is as follows. enter image description here

Community
  • 1
  • 1
JimmyGao
  • 55
  • 1
  • 9
  • 1
    You can either change your x axis limits with `limits`, e.g., `limits = c(0, 45)`, or use something other than 0 values in `expand`. – aosmith Dec 18 '15 at 16:10
  • 1
    [This question](http://stackoverflow.com/questions/27028825/ggplot2-force-y-axis-to-start-at-origin-and-float-y-axis-upper-limit) close relative, maybe not duplicate. – aosmith Dec 18 '15 at 16:29
  • 1
    **Don't** use `data$column` inside `aes()`. It will cause problem if you try to facet or use other advanced features. You should have `aes(x = MeanDecreaseAccuracy, y = reorder(factor(Variables, MeanDecreaseAccuracy)))`. To solve your problem, I would recommend setting `limits = c(0, 1.05 * max(df1$MeanDecreaseAccuracy))`. (Note that is *not* inside `aes()` so you do need to use the `data$column` identifier). – Gregor Thomas Dec 18 '15 at 17:16
  • Hi Gregor,Thank you very much.Your answer works well. – JimmyGao Dec 19 '15 at 09:52
  • So post as an answer? And mark it correct? That is how the site is supposed to work you know. – Mike Wise Dec 19 '15 at 23:24
  • Use 'limits=c(0,45)' – JimmyGao Dec 20 '15 at 14:17

1 Answers1

1

Okay, posting as answer:

Don't use data$column inside aes(). It will cause problems if you try to facet or use other advanced features. You should have

aes(x = MeanDecreaseAccuracy,
    y = reorder(factor(Variables, MeanDecreaseAccuracy)))

To solve your problem, I would recommend setting limits = c(0, 1.05 * max(df1$MeanDecreaseAccuracy)). inside your scale_x_continuous. (Note that is not inside aes() so you do need to use the data$column identifier here).

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294