Randomize
Randomize

Reputation: 9103

Show right values on axis

I have this Data.Frame (mydf):

year   total
1999   3967
2002   580
2005   5203
2008   2406

and to display it, I just run:

plot(mydf)

However I can see on the x-axis (year) the labels:

2000 2002 2004 2006 2008

How can I tell to plot/axis to display only the four expected values: 1999,2002,2005,2008 without trying to infer the sequence?

Upvotes: 1

Views: 80

Answers (2)

akrun
akrun

Reputation: 887098

If we are using ggplot, we can also try

library(ggplot2)
library(dplyr)
mydf %>% 
    mutate(year = as.character(year)) %>% 
    ggplot(., aes(x=year, y = total)) + 
             geom_point() + 
             scale_x_discrete(labels = mydf$year) +
             theme_bw()

enter image description here

data

mydf <- structure(list(year = c(1999L, 2002L, 2005L, 2008L), total = c(3967L, 
580L, 5203L, 2406L)), .Names = c("year", "total"), class = "data.frame", row.names = c(NA, 
-4L))

Upvotes: 2

Zheyuan Li
Zheyuan Li

Reputation: 73285

As akrun suggest, you can use:

plot(mydf, xaxt = "n")
axis(1, at = mydf$year)

Data:

mydf <- structure(list(year = c(1999L, 2002L, 2005L, 2008L), total = c(3967L, 
580L, 5203L, 2406L)), .Names = c("year", "total"), class = "data.frame",
row.names = c(NA, -4L))

enter image description here

Upvotes: 2

Related Questions