Home > OS >  How do I display just one value in the x-axis in ggplot2?
How do I display just one value in the x-axis in ggplot2?

Time:06-21

I have a dataset that contains the following values

       order_dates  Sales in Dollars
1          50           10
2          50           15
3          50           20
4          50           30
5          50           35
5          50           45

When using ggplot2 and geom_point, I want the x-axis to contain just 1 value -- 50. Right now, ggplot2 includes values before 50 as well. Any ideas I can implement to get the desired result?

CodePudding user response:

Plenty of ways to do that. Here suggestions from the comments, as a community wiki.

library(ggplot2)
df <- structure(list(order_dates = c(50, 50, 50, 50, 50, 50), Sales = c(10, 
                                                                        15, 20, 30, 35, 45)), row.names = c(NA, -6L), class = c("tbl_df", 
                                                                                                                                "tbl", "data.frame"))
## define the breaks
## no need for "label" argument
ggplot(df)   geom_point(aes(x = order_dates, Sales))  
  scale_x_continuous(breaks = 50)

## set constant aes (recommended only in very specific cases) as a character
ggplot(df)   geom_point(aes(x = "50", Sales))

## convert the variable to character
df$order_dates <- as.character(df$order_dates)
ggplot(df)   geom_point(aes(x = order_dates, Sales))

Created on 2022-06-14 by the reprex package (v2.0.1)

  • Related