DS4PS / cpp-526-sum-2021

Coure shell for CPP 526.
https://ds4ps.org/cpp-526-sum-2021/
MIT License
1 stars 3 forks source link

Moving X Axis labels Right - Lab 3 #18

Open AshtonHop9 opened 3 years ago

AshtonHop9 commented 3 years ago

Hey everyone,

I have been looking forever for how to shift my labels on the X axis to the right and I cannot find anything! If anyone could point me in the right direction I would really appreciate it!!

Thanks!

jamisoncrawford commented 3 years ago

What graphics package are you using?

AshtonHop9 commented 3 years ago

@jamisoncrawford I didn’t load a graphics package like ggplot2, so I think that means I am just using the standard R graphics package.

lecy commented 3 years ago

What are you currently doing? Some code and current image would be helpful.

AshtonHop9 commented 3 years ago

@lecy Currently I am replicating a NYT graph about baseball strikeout averages, here is some of my beginning code..

plot.new()

par(mar=c(5.1, 4.1, 4.1, 3.1), xpd=TRUE)

plot.window(xlim = c(1900, 2012), # Specify x-axis limits ylim = c(ave.so.min, ave.so.max), xaxs = "i") # Specify y-axis limits

points(x = year, y = ave.so, col = "gray90", xpd = NA, # Color pch = 16, # Shape cex = 0.8) # Size

axis(side = 1, #X axis style at = seq(from = 1900, to = 2020, by = 10), xlim = c(1900, 2020), mgp = c(3,0,0), tcl = -0.2, # Size of the tick mark labels cex.axis = 0.7,
xpd = FALSE)

axis(side = 4, las = 1, # Y axis Style at = c(0,1,2,3,4,5,6,7,9), cex.axis = 0.7, mgp = c(0,1,0), #location of axis labels tick = 0, # Eliminates Tick Line col.axis = "gray75") # Color to be used for tick mark labels

I am trying to shift the x axis values slightly to the right so they are not centered with the ticks.

Screenshot (151)

lecy commented 3 years ago

Try:

axis( … , hadj=2 ) # might be padj

These let you move text around their default placement. Should accept positive and negative values. Zero is probably default.

jamisoncrawford commented 3 years ago

@AshtonHop9 check out the documentation of axis() by typing ?axis. There is an argument listed that will adjust axis labels horizontally (h) or vertically (v).

jamisoncrawford commented 3 years ago

@lecy just beat me to it!

lecy commented 3 years ago

There’s a similar argument for the text() function, slightly different syntax.

adj=c(0.5,0.5)
one or two values in \([0, 1]\) which specify the x 
(and optionally y) adjustment (‘justification’) of 
the labels, with 0 for left/bottom, 1 for right/top, 
and 0.5 for centered. On most devices values 
outside \([0, 1]\) will also work. 

Both use adj for adjust but axis() has two arguments while text() has one with two values.

One downside of having lots of people contributing functions - tiny inconsistencies in syntax that are frustrating when you are learning.

After awhile they become charming, but it takes some time :-)

lecy commented 3 years ago

One more power tip. Type this to get all of the default graphical parameters:

par()
AshtonHop9 commented 3 years ago

@jamisoncrawford @lecy

Thanks! It worked! Also, thanks for the pro tip and additional information. You guys are the real MVPs!