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

Time:04-02

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)

CodePudding user response:

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.

  • Related