This is my first question on stackoverflow. I'm a longtime lurker, and hopefully this is comprehensibly written. So, I've already browsed here for answers and have incorporated these suggestions into my code, but there's evidently something I'm missing or doing wrong.
Issue: I am trying to replace the x axis, which currently displays 0:60 by 10 (10,20,30...), even though x is input in years from 1946:2012, with years instead. Every time I run the code axis() isn't making any changes to the code. I've tried setting axes = FALSE in the plot argument and while this eliminates axes, axis() still does nothing. I also intend to change the Y axis in a similar manner (convert from decimal to percentage). I've considered simply using creating new vectors and inputting these as my X and Y instead, but I would prefer to not make any changes to the database.
Key
unvoting$PctAgreeUS is a numeric decimal value which indicates agreement with US in UN General Assembly in a given year,
unvoting$Year is integer which assigns a year to each observation.
Code
plot(tapply(unvoting$PctAgreeUS,unvoting$Year,mean),
xlab="Year", axes = TRUE,
ylab="Percent Agree", ylim = c(0,.8),
main = "Agreement with the United States",
type="l")
#the x axis
axisyears<-c(unique(unvoting$Year))
axis(1,at = seq(round(min(axisyears)),
round(max(axisyears)),by=1),
labels = 1946:2012)
#the y axis
axisagrmt <-c(unvoting$PctAgreeUS)
axis(2, at = seq(0,8,), by=1,
labels = c("0",".1",".2",".3",".4",".5",".6",".7",".8"))
Thank you
You need to set the at
values to where the labels should be on the current axis scale (i.e. 1:67 on the x axis)
plot(tapply(unvoting$PctAgreeUS, unvoting$Year, mean),
xlab = "Year",
axes = FALSE,
ylab = "Percent Agree",
ylim = c(0,.8),
main = "Agreement with the United States",
type = "l")
axis(1, at = 1:67, labels = 1946:2012)
axis(2, at = seq(0, 0.8, 0.1))
Made up data
unvoting <- data.frame(PctAgreeUS = runif(67 * 4, 0, 1),
Year = rep(1946:2012, each = 4),
country = rep(LETTERS[1:4], each = 67))