I have a barplot and would like to have the label showing different colours and the bars to be grey.
I tried:
col_lab <- c("red","green","grey","red","red","blue")
barplot(1:6,names.arg=1:6,main="barplot",las=1,horiz=TRUE,col="grey",xaxt="n",col.names=col_lab)
but I got an error.
Thank you for your help.
Use yaxt="n" and do an mtext. To get the right positions, use the coordinates barplot throws invisibly.
b <- barplot(1:6, names.arg=1:6, main="barplot", las=1, horiz=TRUE, col="grey",
xaxt="n", yaxt="n")
mtext(1:6, 2, .5, at=b, las=2, col=col_lab, font=2)
Related
I am using the histbackback function from the Hmisc package to generate two back-to-back histograms.
I managed to add colors to the bars, but I did not manage to add density lines to both histograms.
Could someone help me out?
Here is the code I have tried so far.
library(Hmisc)
hbb <- histbackback(pre, post, brks=seq(0.0001, 0.20, by=0.01))
barplot(-hbb$left, col="lightgrey", horiz=TRUE, space=0, add=TRUE, axes=FALSE)
barplot(hbb$right, col="darkgrey", horiz=TRUE, space=0, add=TRUE, axes=FALSE)
# works until here but now I cannot add lines
lines(density(pre), # density plot,
horiz=TRUE, space=0, add=TRUE, axes=FALSE)
I'm creating a graphic that has a few different graph elements, using layout() to define plotting regions. I have a separate region for labels that need to align to bars on a barplot in an adjacent plotting region.
I can take a guess at where to plot the labels so that they line up - but the number of these locations will vary so this is not an ideal solution.
Here's an example of what I'm trying to do:
labs <- paste("Some text", letters[1:9])
datA <- table(sample(letters[1:9], size=200, replace=TRUE, prob=rep(c(0.1,0.2,0.3),3)))
layout(matrix(c(1,2,3,3), 2, 2, byrow=TRUE), widths=c(1,2), heights=c(6,1))
plot.new()
text(x=1, y=seq(0.05,1.0,0.111), labels=labs, adj=1, cex=1.4)
barplot(datA, horiz=TRUE, las=1, axes=F, yaxt="n")
How can I find the correct values to plot the labels?
(I'm aware that it looks like this can be solved by just plotting the labels with the barplot - this is not a viable solution for what I'm doing).
The output of barplot gives the heights so:
bp <- barplot(datA, horiz=TRUE, las=1, axes=F, yaxt="n")
text(0*bp, bp, labs, col = "blue", pos = 4)
I have a following "beeswarm" (a single-dimensional scatterplot)
library(beeswarm)
data(breast)
beeswarm(breast$time_survival,horizontal=TRUE)
Here is the resulting plot:
How can I get rid of the axes and the box around the plot, so that I can reintroduce only the X axis and nothing else around it?
If you create an empty plot first
plot(rnorm(10), type="n", axes=FALSE, xlim=c(0, 200), ylim=c(0.4, 1.6),
xlab="", ylab="")
Then you can use the add argument to get what you want
beeswarm(breast$time_survival,horizontal=TRUE, add=TRUE)
You can use the "axes" argument (described in ?plot.default).
beeswarm(breast$time_survival, horizontal=TRUE, axes = FALSE)
I am having trouble getting the spacing right on a plot on top of a horizontal barplot. It is the same general issue as described here:
http://www.r-bloggers.com/adding-lines-or-points-to-an-existing-barplot/
But I am trying to use "plot" instead of "points" or "lines". Is there a trick for using plot to get the spacing of the bars and the points to match?
Code:
barplot(df$DIC_mM,col=scalegreen, xlab="DIC mM", horiz=TRUE, xlim=c(0,0.7),
col.axis="white", col.lab="white", axes=FALSE, border="white")
axis(1,line=1,col="white",col.ticks="white",col.axis="white")
par(new = TRUE)
plot(df$d13DIC,df$Order, type="p", axes = FALSE, bty = "n", xlab ="",
col="deepskyblue2", lwd=5, xlim=c(-50,170), lend=2, col.lab="white", ylab="")
axis(3,at = c(-50,0,50,100,150), line=1, col="deepskyblue2", col.ticks="deepskyblue2",
col.axis="deepskyblue2")
mtext(expression(paste(delta ^{13},'DIC'," \u0028","\u2030","\u0029")), 3,
line=-0.5,at=50,col="deepskyblue2", cex=0.75)
Is there a reason why you don't want to use points to add the points? If you're willing to use points you can do it like this:
Create barplot and save the y-coordinates of the bars to y. You haven't provided sample data, so I'll use the built-in mtcars data frame:
y = barplot(mtcars$mpg[1:10], horiz=TRUE)
Now add the points. We use y as the y values, because those are the coordinates of the midpoints of each bar:
points(sqrt(mtcars$mpg[11:20]), y, col="red", pch=16, cex=2)
When you use par(new=TRUE) and then call plot again, you're overlaying a new plot with a new coordinate system that in general will be different from the original coordinate system.
This is what worked, based on this post suggested by eipi10: midpoints returned by barplot function do not actually line up with midpoints of bars
mp<-barplot(df$DIC_mM,col=scalegreen, xlab="DIC mM", horiz=TRUE, xlim=c(0,0.7), col.axis="white", col.lab="white", axes=FALSE, border="white", ylim=c(0,length(df$DIC_mM)+2))
axis(1,line=1,col="white",col.ticks="white",col.axis="white")
par(new = TRUE)
plot(df$d13DIC, mp, type="p", axes = FALSE, bty = "n", xlab ="",col="deepskyblue2", lwd=5, xlim=c(-50,170), lend=2, col.lab="white", ylab="", ylim=c(0,length(df$DIC_mM)+2))
axis(3,at = c(-50,0,50,100,150),line=1,col="deepskyblue2",col.ticks="deepskyblue2",col.axis="deepskyblue2")
mtext(expression(paste(delta ^{13},'DIC'," \u0028","\u2030","\u0029")),3,line=-0.5,at=50,col="deepskyblue2", cex=0.75)
Newbie Question!
I'm trying to plot a line graph, but the tick-labels on the x-axis have to be non-numeric. How can I do this? So far I have this:
plot(disparitybin$Variance[1,], type="l", xlim=c(0,5.0), ylim=c(0, 0.05), frame=FALSE, xlab="Time", xaxt='n', yaxt='n', ylab="Dispairty (Sum of Variance)", main="Dispairty Through Time", cex.lab=1.25, cex.main=1.75)
axis(1, pos=0, las=0)
axis(2, pos=0, las=2)
I'm trying to specify the names of each tick mark along the x-axis: Eocene, Oligocene, Miocene, Pliocene. How do I get the labels control for the axis() command to accept this info?
Thanks for your help!
Use axis as follows:
axis.labels <- c("Eocene", "Oligocene", "Miocene", "Pliocene")
axis(1, labels=axis.labels, pos=0, las=0)
This assumes that you are plotting exactly four points of data. Also, you were correct to make the call to axis after the call to plot.