Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

For geom_line with integer variable, not all x labels and vertical white lines are displayed

I want to show all the labels on the x-axis in the trendline chart. I tried specifying the labels with x_scale_continous, but they are not displayed either. Also, some lines are now displayed in the default palette of ggplot, i.e. whole lines only appear from x = 1. You can test this with the following example:

df <- data.frame(x = -3:2, y = c(4L, 5L, 6L, 2L, 3L, 1L))
ggplot(df,aes(x,y))+geom_line()+scale_x_continuous(labels=as.character(x),breaks=x)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When you add ggproto objects like layers, scales and co-ordinates to a ggplot, you are not actually passing your data frame through to them, and there is no non-standard evaluation or data masking going on.

This means that although in some ways the syntax feels similar to sequential piped functions that you might use in dplyr, you do not have unqualified access to the columns in your data frame inside functions like scale_x_continuous. You therefore have to specify df$x

ggplot(df, aes(x, y)) +
  geom_line() +
  scale_x_continuous(breaks = df$x)

enter image description here

The fact that your own code doesn’t throw an error suggests that you have a global variable called x that is being used inside scale_x_continuous instead.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading