0

Hi i have to make a simple graph using r, but I would like to make it look good, I am also having some trouble labeling my y axis, what I've written so far is this:

anim <- c(1.72,1.6,1.7,1.74,1.8)
mirr <- c(.80,.95,1.13,1.17,1.26)
g_range <- range(0.8, anim, mirr)
plot(anim, type="o", col="blue", ylim=g_range, axes=FALSE, ann=FALSE)
axis(1, at=1:5, lab=c(".5", ".6", ".7", ".8", ".9"))
axis(2, las=2, at=0:g_range[2])
axis(2)
box()
lines(mirr, type="o", pch=22, lty=2, col="red")
title(main="corr vs dprime", col.main="forestgreen", font.main=4)
title(xlab="Correlation", col.lab="black", font.main=4)
title(ylab="dprime", col.lab="black", font.main=4)
legend(1, g_range[2], c("anim", "mirr"), cex=0.8,
col=c("blue","red"), pch=21:22, lty=1:2);

which gives this graph:

enter image description here

I can't figure out how to make the graph area big enough to comfortably fit the legend in, and how to correctly label my y axis, I have no idea where the sideways 1 has come from? r newbie so any help would be great!

rawr
  • 20,481
  • 4
  • 44
  • 78
queenslug
  • 103
  • 4
  • 2
    you could use `ylim=g_range + c(0, .2)` instead of what you have now. there are two y-axes because you are doing `axis(2, ...)` twice – rawr Mar 24 '15 at 23:29
  • 1
    Move your legend down. See `?legend`. Use the x and y parameters. To change the orientation of axis tick labels read the help page for ?par (and find the 'las'-parameter). – IRTFM Mar 24 '15 at 23:44

2 Answers2

2

If you choose to stay with the base plotting functions in R, you can address each of your concerns. The following response is simply meant to review and illustrate previous comments from this question and include other SO-related materials for additional assistance.

First, let's tackle the y-axis issue. As suggested in the original comments, you are currently calling the axis command twice with respect to the y-axis. By identifying your desired y-axis limits early on, they can be easily applied with the ylim argument.

df<-data.frame(anim=c(1.72,1.6,1.7,1.74,1.8),mirr=c(.80,.95,1.13,1.17,1.26))
gmin<-min(df) #<---- ESTABLISH MIN Y-AXIS VALUE
gmax<-max(df) #<---- ESTABLISH MAX Y-AXIS VALUE
plot(df$anim,type="o",col="blue",ylim=c(gmin,gmax),axes=FALSE,ann=FALSE)
box()
axis(1,at=1:5,lab=seq(0.5,0.9,0.1))
axis(2,seq(gmin,gmax,0.2),las=2)
lines(df$mirr,type="o",pch=22,lty=2,col="red")
title(main="corr vs dprime",col.main="forestgreen",font.main=4)
title(xlab="Correlation",col.lab="black",font.main=4)
title(ylab="dprime",col.lab="black",font.main=4)

Image 1

Second, the legend can be placed either within the plotting area or placed outside. As suggested in the original comments, type in ?legend and review the x, y arguments for positioning your legend. This would likely be your best approach since your range of x and y values are small, which would allow for easy legend placement. Although less specific, you can also use keywords to specify the legend's position (?legend under Details) such as "bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right" and "center". For instance:

legend('bottomright', c("anim", "mirr"), cex=0.8,col=c("blue","red"), pch=21:22, lty=1:2)

Image 2 - Legend using keywords

where adjusting arguments, such as x.intersp, y.intersp, and cex, will allow you to change the legend's shape and relative size.

Additionally, you can create your legend outside of the plotting area as shown by the previous answer using ggplot2. Several options are recommended in the following SO answer: Plot a legend outside of the plotting area in base graphics?. The most popular methods take advantage of the xpd argument in par followed by the x, y or inset arguments from legend.

Without adjusting margins about the plotting area, you can simply select x and y parameters so the legend is placed properly while not overlapping other items. For instance:

par(xpd=TRUE)
legend(4.5,2.1,c('anim','mirr'),cex=0.8,col=c('blue','red'),pch=21:22,lty=1:2)

enter image description here

where the box about the legend can be controlled using the bty argument in legend.

If you would prefer your legend more like the previous answer, you will need to review the mai, mar, and oma arguments in par so that the legend is properly displayed (see here). Once configured, you can then specify your legend's position outside of the plot area using inset argument in legend:

par(oma=c(0,0,0,1),mar=c(5.1,4.1, 4.1, 5.1),xpd=T)
plot(df$anim,type="o",col="blue",ylim=c(gmin,gmax),axes=FALSE,ann=FALSE)
box()
axis(1,at=1:5,lab=seq(0.5,0.9,0.1))
axis(2,seq(gmin,gmax,0.2),las=2)
lines(df$mirr,type="o",pch=22,lty=2,col="red")
title(main="corr vs dprime",col.main="forestgreen",font.main=4)
title(xlab="Correlation",col.lab="black",font.main=4)
title(ylab="dprime",col.lab="black",font.main=4)
legend('right',inset=c(-0.4,0),c('anim','mirr'),cex=0.8,col=c('blue','red'),pch=21:22,lty=1:2)

enter image description here

Alongside other answers, I hope this small review helps your current situation and demonstrates R's flexibility using base and other plotting functions. Credit goes to previous users and their comments along with the other SO answers.

Community
  • 1
  • 1
ccapizzano
  • 1,556
  • 13
  • 20
1

I'd use ggplot2:

anim <- c(1.72,1.6,1.7,1.74,1.8)
mirr <- c(.80,.95,1.13,1.17,1.26)

df <- data.frame(
dprime = c(anim, mirr),
correlation = rep(seq(.5,.9,.1), 2),
group = rep(c("anim", "mirr"), each=5)
)
# Uncomment if you don't have ggplot2 installed
# install.packages("ggplot2")

library(ggplot2)

g <- ggplot(df, aes(correlation, dprime, color=group)) + 
  geom_line() +
  geom_point() +
  ggtitle("Corr vs. Dprime")
g

enter image description here

Alex Coppock
  • 2,122
  • 3
  • 15
  • 31