I've got a matrix that I've plotted using the bar plot function to create a stacked bar plot of changes in percent of something (alphabets) over time. The matrix looks like:
Year V W X Y Z
1 7.5397 20.6349 11.1111 0.3968 60.3175
2 8.3333 21.4286 11.9048 0.3968 57.9365
3 23.8095 9.5238 9.5238 0.3968 56.7460
4 23.4127 10.7143 10.3175 1.1905 54.3651
5 23.4127 11.1111 10.7143 1.9841 52.7778
6 30.0000 2.4000 19.2000 2.4000 46.0000
All these should be the same height, because they add to 100, but in my plot, all the heights are just a bit different at the top. My bar plot code looks like this:
>barplot(t(as.matrix(mydata[,2:6])),names.arg=unique(mydata$Year),axis.lty=1, ylim=c(0,100),
xlab="Year", ylab="Percent of Categories", etc.
I've tried adjusting the ylim, changing xpd to =FALSE, and making some axis argument adjustments, and I'm not sure what else to try before scratching the whole thing and retrying with a different plotting function. Any help is appreciated.
Related
I have some discrete data that I'm trying to plot in a histogram in R. I'm using the built in hist() function, which works fine most of the times for the data I have. However, when it comes to a discrete variable it looks somewhat strange (unfortunately I cannot add the picture). I interpret it as "since the bin for 0 and 1 children must fit between 0 and 1 it determines the width of all bins and thus the "from 1.5 to 2" result". How can I put the numbers on the x-axis centered underneath each bin instead?
Thanks in advance!
You might want to consider drawing the axis in a second step:
Prevent x-axis with xaxt="n":
hist(cars$speed, xaxt="n")
Draw x-axis and adjust label position with hadj=:
axis(1, at=seq(0,25,5), hadj=2.5, labels = c("",seq(5,25,5)))
delme <- exp(rnorm(1000,1.5,0.3))
boxplot(delme,log="y")
boxplot(log10(delme))
Why are the whiskers different in this 2 plots?
Thanks
Agus
I would say that in your first plot you just changed the y axis to log, so the values you plot still range between 1 and 10. In this plot the y axis is a log scale. The whiskers on this axis look different because the space between each "tick" (ie axis break) is not constant (there is more space between 2 and 4 than between 10 and 8)
In the second plot, you take the log of the values then plot them, so they range from .2 to 1, and are plotted with a linear y axis.
Look at the summary for both of the normal and log transformed dataframes
This is my script and the graph produced. I have made a gap between 7-29.8. But How can I display the y-axis values at 7 and 30? The axis only shows 1-6, instead of 0-7 , 30 as intended.
gap.boxplot(Km, gap=list(top=c(7,30), bottom=c(NA,NA), axis(side=2, at=c(0,29.8), labels= F)),
ylim=c(0,30), axis.labels=T, ylab="Km (mM)", plot=T, axe=T,
col=c("red","blue","black"))
abline(h=seq(6.99,7.157,.001), col="white")
axis.break(2, 7.1,style="slash")
You can call the axis function again to plot the marks at c(0:7,30)
axis(2,c(0:7,30)
But because you have a gap between 7 and 30, anything beyond 7 will have to be shifted in the plot. In general, a mark at position y will have to be moved downwards to y-gap.width, or y-(30-7) in your case.
So you can do this to plot your marks:
axis(2, labels=c(0:7,30), at=c(0:7,30-(30-7)))
It's hard to replicate the plot without example data. But I think this worth a try and should work.
axis(2,labels=c(0:7), at=c(0:7)) # build first gap marker '7'
then separately add the second gap marker
axis(2, labels=c(30), at=7*(1+0.01)) # the interval (0.01) could be different, test to find the best one to fit your plot
I have two vectors: x and y. I'm plotting them with plot(x,y, type="l"). However I want to show the detailed values on the x-axis between the plotted values on the x-axis. Now I have 0 ... 20 ... 40 ... I want to show 0 1 2 3 4 5 6 ... 20 and I want them to be in smaller size than the main values. How can I do that?
Here is the answer to your question
grid = 1:100
x = rnorm(100)
plot(x,type='l')
axis(1,grid[c(-20,-40,-60,-80,-100)],grid[c(-20,-40,-60,-80,-100)],cex.axis=.5,line=-1,tick=FALSE)
Use axis to customize it. For example:
plot(seq(1,100,10), rnorm(10),type='l',cex.axis=2,
lwd.ticks=5)
axis(1, 1:100[-c(20,40,60,80,100)],
1:100[-c(20,40,60,80,100)],tick=TRUE,
cex.axis=0.8)
Looking to have the values of x-axis plotted in the center of the bars in R.
Having issues finding a way to make this possible, code is below:
hist(sample_avg, breaks =7, ylim=c(0,2000),
main = 'Histogram of Sample Average for 1 Coin Flip', xlab= 'Sample Average')
This is just for a coin flip, so I have 6 possible values and want to have 6 buckets with the x-axis tick marks underneath each respective bar.
Any help is much appreciated.
hist() returns the x coordinate of the midpoints of the bars in the mids components, so you can do this:
sample_avg <- sample(size=10000,x=seq(1,6),replace=TRUE)
foo <- hist(sample_avg, breaks =7, ylim=c(0,2000),
main = 'Histogram of Sample Average for 1 Coin Flip', xlab= 'Sample Average',
xaxt="n")
axis(side=1,at=foo$mids,labels=seq(1,5))
# when dealing with histogram of integers,
# then adding some residual ~ 0.001 will fix it all...
# example:
v = c(-3,5,5,4,10,8,8)
a = min(v)
b = max(v)
foo = hist(v+0.001,breaks=b-a,xaxt="n",col="orange",
panel.first=grid(),main="Histogram of v",xlab="v")
axis(side=1,at=foo$mids,labels=seq(a,b))
Not as nifty as you might have been hoping, but looks like the best thing is to use axes=F, then put in your own axes with the 'axis' command, specifying the tick marks you want to see.
Reference: https://stat.ethz.ch/pipermail/r-help/2008-June/164271.html