How do I label each bar below the x-axis, for example if each bar represents a month, how do I get month one, month two, month three etc below each bar.
conditiongood <- c(50,65,60,65,59)
conditionpoor <- c(61,46,51,46,52)
condition <- rbind(conditiongood,conditionpoor)
layout(matrix(1:1,1,1))
barplot(as.matrix(condition),
col=c("darkblue","red"),
xlab="month",
ylab="subject count",
main="Monthly condition",
ylim=c(0, 140))
legend(5.25,140.1,
c("good","poor"),
fill=c("darkblue","red"),
title="condition")
You mean something like this?
Which is based on this post.
There are probably more sophisticated ways to do this.
# Barplot
bp<-barplot(as.matrix(condition),
col=c("darkblue","red"),
xlab="month",
ylab="subject count",
main="Monthly condition",
ylim=c(0, 140))
# x-axis labels
axis(1, at = bp,
labels=c("month 1", "month 2", "month 3", "month 4", "month 5"),
cex.axis=1.2)
# Add legend
legend(5.25,140.1,
c("good","poor"),
fill=c("darkblue","red"),
title="condition")
Which will give:
You probably want to do something on the position of your legend, and I don't think that specifying the xlab is necessary if you're going to label each bar individually.
I won't comment on the choice of colours :)
Related
I don't know how to identify the category of a certain symbol in a plot when plotting nmds results by certain environmental variable. I determine the plotted data in the pch section, so I cannot choose the symbols manually. In ympm$hakkuutyyppi there are values 1-3 depending on the treatment method.
The code I used:
par(mar=c(0,1.5,1.5,0), oma=c(1.5,0,0,0.2))
plot(myrkky.ndms, display="sites", type="n", xlab=NA, ylab=NA, xaxt="n", yaxt="n")
mtext(side=1, "Axis 1", line=0.2, cex=0.8)
mtext(side=2, "Axis 2", line=0.2, cex=0.8)
points(myrkky.ndms, "sites", pch=6-2*ympm$Hakkuutyyppi, cex=1.5)
So what I want to know is how to determine the symbol of certain treatment, or how I can know what the printed symbols represent?
Thank you!
I am plotting roughly 300 variables, R automatically sets x-axis labels and has them as numbers, I don't want this. But if I use the xaxt = 'n' feature and then 'axis', it won't work because for axis I'll need to define x labels for 300 variables to....
Is there any way to fix this?
To give a better example of my issue, my code looks something like this:
plot(data, xaxt = 'n')
x = c("Time 1", "Time 2", "Time 3", "Time 4", "Time 5", "Time 6")
axis(side = 1, at = 1:6, labels = x[1:6])
But this is not working, I think it has to do with the fact that I have 300 data points, not 6, but I just want it to equally space out these 6
at should contain 6 numbers between 1 and 300 at which you want the labels to be placed, not just 1 through 6, because this will squash them all at the beginning/left side.
dat=rnorm(300)
plot(dat, xaxt="n")
x=c("Time 1", "Time 2", "Time 3", "Time 4", "Time 5", "Time 6")
axis(1, at=floor(seq(1,300,length=6)), labels=x)
Is there a way to change the font size of axis labels when plotting hexbin data?
library(hexbin)
myData <- hexbin(rnorm(100), rnorm(100))
myPlot <- plot(myData, xlab = "Variable 1", ylab = "Variable 2")
You can suppress the labels and add them separately with grid commands. It may take some trial and error to position them exactly where you want them:
library(grid)
myPlot <- plot(myData, xlab="", ylab="", lcex=.75)
grid.text("Variable 1", .45, .1, gp=gpar(fontsize=12))
grid.text("Variable 2", .05, .5, rot=90, gp=gpar(fontsize=12))
I am trying to make a grouped bar chart. Right now this is what I have:
Grouped Bar Chart
For background: I have quite a large data set with multiple variables. What I am interested in for this bar chart is visually representing the median inspection distance (cm) that male guppies (yes, fish) will inspect a predator in the presence and absence of females. As you can see, below the two bar charts there is "A" and "B".... I want these to say "Bright" and "Drab"... I cannot seem to get anything to work!!
this is my code right now:
barplot(matrix(c(18.41,7.20,21.40,11.17),nr=2), beside=T,
col=c("aquamarine3","snow3"), ylim=c(0, 25),
names.arg=LETTERS[1:2], xlab = "Colour", ylab = "Inspection Frequency (cm)")
legend("topright", c("Present","Absent"), pch=15, col=c("aquamarine3","snow3"),
bty="n")
thank you in advance - I know this is a super basic question but I am fairly new at this!
You can suppress the plotting of the x axis labels and then add labels of your own. To see where barplot draws each of the bars, save it to an object.
myplot <- barplot(matrix(c(18.41,7.20,21.40,11.17),nr=2),
beside=T, xaxt="n",
col=c("aquamarine3","snow3"), ylim=c(0, 25),
names.arg=LETTERS[1:2], xlab = "Colour",
ylab = "Inspection Frequency (cm)")
axis( 1, at=colMeans(myplot), labels=c("Bright","Drab"))
I want to plot two outcomes of a behavioral psychological experiment in one plot: performance (as measured by % of correct answers) through a marplot and reaction times (in ms) through a line plot.
I've figured everything out except one thing: the ticks on the x-axis and the dots in the line plot don't align with the center of the bars. Someone suggested to save the barplot as an object and then use "at=" which works for the ticks on the x-axis but not for the dots in the line plot. I also wasn't able to make it work by using the "lines" function since the scales are different (.7-1.0 on the left for the barplot and 400-900 on the right for the line plot).
par(mar=c(6, 4, 4, 4))
m<-barplot(c(0.87,0.83,0.79),ylim=c(0.7,1),xpd=FALSE,ylab="% correct")
axis(1,at=m,labels=c("cond. A", "cond. B", "cond. C"))
par(new=T)
plot(c(720, 800, 830), pch=15, ,ylim=c(400,900), xlab="", ylab="",
axes=F, type="b")
mtext("reaction time [ms]",side=4,line=2.5)
axis(4, ylim=c(400,900))
Thank you for any kind of help
On your second plot, set the xlims equal to those of the barplot. Use par('usr') to get the current limits (vector of length 4; xmin, xmax, ymin, ymax). Then when you plot the line, you can use m as the x location and it will line up because your xlims also line up.
par(mar=c(6, 4, 4, 4))
m<-barplot(c(0.87,0.83,0.79),ylim=c(0.7,1),xpd=FALSE,ylab="% correct")
axis(1,at=m,labels=c("cond. A", "cond. B", "cond. C"))
xlims <- par('usr')[1:2] # <-- get xlims
par(new=T)
plot(m, c(720, 800, 830), # <-- supply x coords
pch=15, ,ylim=c(400,900), xlab="", ylab="",
axes=F, type="b", xlim=xlims) # <-- supply xlims
mtext("reaction time [ms]",side=4,line=2.5)
axis(4, ylim=c(400,900))