0

This is the first time I have posted. Please excuse me if this question has been answered elsewhere, or if it is poorly explained.

> summary(pivoteddata)
          country       year               value      
 Belgium      :35   Length:175         Min.   :54.50  
 France       :35   Class :character   1st Qu.:80.30  
 Italy        :35   Mode  :character   Median :86.10  
 Spain        :35                      Mean   :82.37  
 United States:35                      3rd Qu.:87.55  
 Afghanistan  : 0                      Max.   :95.50  
 (Other)      : 0                      NA's   :164 



   > dput(head(pivoteddata, 20))
structure(list(country = structure(c(10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L), .Label = c("Afghanistan" - deleted countries - "Zimbabwe"), 
class = "factor"), year = c("X1984", "X1985", "X1986", 
"X1987", "X1988", "X1989", "X1990", "X1991", "X1992", "X1993", 
"X1994", "X1995", "X1996", "X1997", "X1998", "X1999", "X2000", 
"X2001", "X2002", "X2003"), value = c(NA, NA, NA, NA, NA, NA, 
NA, NA, 90.8, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), 
row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"))

This is the code for my scatter graph: -

practiceplot <- ggplot(pivoteddata, aes(x=year, y=value, group=country, 
                        color=country, size=10)) +
  geom_line() + geom_point()

...and what it looks like: -

X axis using original values

I want to change the x-axis values, so added to the code :-

ggplot(pivoteddata, aes(x=year, y=value, group=country, 
                        color=country, size=10)) +
  **scale_x_discrete(breaks = c(5, 10, 15, 20, 25, 30, 35), 
                     labels = c("1985", "1990", "1995", "2000", "2005", 
                                "2010", "2015"))**

The new values do not appear on the x-axis. What am I doing wrong?

Thanks

enter image description here

  • Can you post sample data? Please edit **the question** with the output of `dput(pivoteddata)`. Or, if it is too big with the output of `dput(head(pivoteddata, 20))`. – Rui Barradas Jan 26 '21 at 12:19
  • > summary(pivoteddata) country year value Belgium :35 Length:175 Min. :54.50 France :35 Class :character 1st Qu.:80.30 Italy :35 Mode :character Median :86.10 Spain :35 Mean :82.37 United States:35 3rd Qu.:87.55 Afghanistan : 0 Max. :95.50 (Other) : 0 – Eloise Phelps Jan 26 '21 at 12:45
  • Thanks for the summary, but that's not enough to plot the data. Please post the output of the `dput` command like stated in my first comment. – Rui Barradas Jan 26 '21 at 12:50
  • > dput(head(pivoteddata, 2)) class = "factor"), year = c("X1984", "X1985"), value = c(NA_real_, NA structure(list(country = structure(c(10L, 10L), .Label = c("Afghanistan", "Albania", "Algeria"...), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame")) – Eloise Phelps Jan 26 '21 at 12:50
  • Almost there! Those are only 2 values, not 20. And the beginning of the output is missing. – Rui Barradas Jan 26 '21 at 12:52
  • Apologies - I tried to upload 20, but it's far too long. – Eloise Phelps Jan 26 '21 at 12:59
  • OK, that's because `country` is a factor, and `dput` will include all of its levels, even if they are not present in the selected data set. But what you have posted is enough for us to have an idea of the data structure. – Rui Barradas Jan 26 '21 at 13:02
  • > dput(head(pivoteddata, 20)) structure(list(country = structure(c(10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L), Label = c("Afghanistan" - deleted countries - "Zimbabwe"), class = "factor"), year = c("X1984", "X1985", "X1986", "X1987", "X1988", "X1989", "X1990", "X1991", "X1992", "X1993", "X1994", "X1995", "X1996", "X1997", "X1998", "X1999", "X2000", "X2001", "X2002", "X2003"), value = c(NA, NA, NA, NA, NA, NA, NA, NA, 90.8, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame")) – Eloise Phelps Jan 26 '21 at 13:03
  • Try `pivoteddata$year – Rui Barradas Jan 26 '21 at 13:06
  • @Rui Barradas. Thank you. I'm sorry my comment was so poorly defined. Thank you very much for your patience with me. And thank you for answering my question! You're the best. If you post it as an answer I will accept as a solution and upvote you – Eloise Phelps Jan 26 '21 at 13:25

2 Answers2

0

Without having your data, I would say you should use scale_x_continuous() instead of scale_x_discrete()

pascal
  • 786
  • 2
  • 13
0

You should post a reproducible example to know what class(year) is. Assuming that's a factor, try this:

ggplot(pivoteddata, aes(x=year, y=value, group=country, 
                        color=country, size=10)) +
  scale_x_discrete(breaks = c("1985", "1990", "1995", "2000", "2005", 
                              "2010", "2015"), 
                   labels = c("1985", "1990", "1995", "2000", "2005", 
                              "2010", "2015"))
Leonardo
  • 1,630
  • 1
  • 6
  • 19