I have created the following barplot
, but the problem is that the legends doesn't have the right color, and the grids are in front of the bars. I would like to put the right columns in the boxes at the legend, and put the grid behind the bars.
I also would like to put a tick in the bottom of each bar just to identify them
How can I do these features in R?
The r script that I use:
autos_data <- read.table("data.dat", header=T, sep="\t")
barplot(rep(NA,length(autos_data)),ylim=c(0,max(autos_data)),axes=FALSE)
barplot(t(as.matrix(autos_data)), main=NULL, ylab= "% of Cloud usage", xlab="Input data size (MB)", ylim=c(0,50), beside=TRUE, lwd=1:2, angle=c(45,135), density=seq(5,40,10), col=c("blue","red","black","darkgreen"))
grid(NA, 5, lwd=1,lty=5, col="gray") # grid only in y-direction
# Place the legend at the top-left corner with no frame
coords="top"
legend(coords, c("WestVirginia","Chicago I","Chicago II","California"), text.col=par("col"), cex=1.2, bty="n", xpd=TRUE, horiz=TRUE, inset=c(0,-.22), angle=c(35,135,45,135), density=seq(5,40,10),col=c("blue","red","black","darkgreen"))
Edit:
dput(autos_data)
structure(list(WestVirginia = c(29L, 29L, 23L, 23L), ChicagoI = c(30L,
21L, 36L, 26L), ChicagoII = c(39L, 42L, 35L, 46L), California = c(2L,
8L, 6L, 5L)), .Names = c("WestVirginia", "ChicagoI", "ChicagoII",
"California"), class = "data.frame", row.names = c("1500", "3000",
"4500", "6000"))
The data:
West-Virginia ChicagoI ChicagoII California
1500 29 30 39 2
3000 29 21 42 8
4500 23 36 35 6
6000 23 26 46 5
Open Excel's Format Legend pane by right-clicking the legend in a chart and selecting "Format Legend." Click the window's Fill and Line icon, shaped like a paint bucket, followed by "Fill." Click the "Color" drop-down menu to view a list of colors.
We can try to add legend to the scatterplot colored by a variable, by using legend() function in Matplotlib. In legend(), we specify title and handles by extracting legend elements from the plot.
The Color Legend is a small, multi-line block of text describing what the colors in the structure mean (Fig.
Add a color element by clicking on Add Color Element and selecting New Color Legend... from the popup menu. In the Add Color Element window, enter the title of the color legend, select a background color and make other necessary formatting settings, and then click OK to confirm. Repeatedly add all the elements needed.
This is an inconsistency of parameter names.
You need to set fill
:
legend(coords, c("WestVirginia","Chicago I","Chicago II","California"), text.col=par("col"),
cex=1.2, bty="n", xpd=TRUE, horiz=TRUE, inset=c(0,-.22), angle=c(35,135,45,135),
density=seq(5,40,10), fill=c("blue","red","black","darkgreen"))
You can compute the legend directly in the barplot
call, specifying the arguments via args.legend
parameter and the text via legend.text
. For the grid, you need to replot the barplot
after setting the grid, with white and then the lines so the grid doesn't appear anymore in the lines:
It needs 3 calls of the same barplot
but I think it gives you what you want:
# first plot
barplot(t(as.matrix(autos_data)), main=NULL, axes=FALSE, beside=TRUE, col="white", ylab= "% of Cloud usage", xlab="Input data size (MB)", ylim=c(0,50))
# add the grid
grid(NA, 5, lwd=1,lty=5, col="gray") # grid only in y-direction
# hide the grid with white bars
barplot(t(as.matrix(autos_data)), main=NULL, beside=TRUE, col="white", ylab= "% of Cloud usage", xlab="Input data size (MB)", ylim=c(0,50), add=TRUE)
# plot the density lines, the legend, etc.
barplot(t(as.matrix(autos_data)), main=NULL, axes=FALSE, beside=TRUE,
lwd=1:2, angle=c(45,135), density=seq(5,40,10), col=c("blue","red","black","darkgreen"),
legend.text=c("WestVirginia","Chicago I","Chicago II","California"),
args.legend=list(x="top", y=NULL, bty="n", ncol=4), add=TRUE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With