I want to plot single bar in a graph so it would look like picture below
I created test data and calculate its mean.
value <- c(99,44,100,120,88)
value_mean <- mean(value)
And plot them using below code
barplot(value_mean, horiz=T, width=30, cex.names=0.5 ,ylim=c(0,200), col="red")
Buth the output is not even close.
I've also looked at this links
Single bar barchart in ggplot2, R
R Barplot with one bar - how to plot correctly
So my output should be something like the first picture. I was thinking that could solve ggplot.
If everything else fails, you can draw a rectangle like this:
par(mar = c(12, 2, 12, 2))
plot(0, type="n", ylim=c(-1, 1), xlim=c(0, 200), axes = F, bty = "n",ylab="", xlab="label")
rect(0, -.7, value_mean, .7, col="red", border=NA)
text(value_mean, 0, pos=4, label=value_mean)
axis(1, at=seq(0, 200, by=40))
Related
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 am trying to plot the barchart and want to show the percentage on horizontal line end.. Not able to get ..whats the mistake
y <- c('Others(27%)','Environment','Family Life & children','Combating terrorism','Pensions',
'Europe', 'Housing', 'Education', 'Tax','Benefits','Immigration & Assylum','Health','Economy')
x <- c(27,10,12,15,18,20,20,21,21,25,42,46,49)
mylabels <- c('....27%','10%','12%','15%','18%','20%','20%','21%','21%','25%','42%','46%','49%')
par(mar=c(5,10,4,2)+0.1)
usr <- par( "usr" )
my_bar<-barplot(x,main="British Voters Election Priority (2015)", horiz=TRUE,las = 1,
names.arg=y, xlim=c(0, 60), border = 'black',
col=rev(topo.colors(14)),
xlab="Percentage (%)")
abline(v=x, col='grey', type="l", lty=2)
#text(myvect1, b[1,], mylabels, col="white", adj=1)
text(my_bar, mylabels, cex=1)
If you want the numbers to be displayed at the end of the bars, just extract the numeric characters out of "mylabels" to get the appropiate x-coordinates and set pos = 4 to display them on the right of the bars. As for the colours for the bars, just pick some palette that suits your wishes or specify all 13 colors manually. There are tons of cheatsheets throughout the web with different color patterns for R.
my_bar<-barplot(x,main="British Voters Election Priority (2015)", horiz=TRUE,las = 1,
names.arg=y, xlim=c(0, 60), border = 'black',
col = palette(rainbow(13)),
xlab="Percentage (%)")
abline(v=x, col='grey', type="l", lty=2)
#text(myvect1, b[1,], mylabels, col="white", adj=1)
text(as.numeric(gsub("\\D", "", mylabels)), my_bar, mylabels, cex=1, pos = 4)
You did not specify x-coordinates for where the tekst should be plotted. Your code now took the values of my_bar for both the x-coordinates and the y-coordinates. If I am correct you want something like this.
Just use this line of code at the end in order to get that:
text(60, my_bar, mylabels, cex=1, pos = 2)
I am relatively new to R. I am making an R Shiny app, and based on the input of the user, I would like to analyze the data and output a bar that shows the Jaccard index. This is what I want it to look like, although obviously a smooth gradient:
Please note that the Jaccard index (in this case, 0.35) will change after each input, so I'd like something reactive. I just have no idea where to start or if making plots like this is even possible in R.
Thanks.
edit: I used an online gradient generator to come up with this plot instead: how could I overlay a vertical line with its corresponding Jaccard index and corresponding location on the bar on this particular image?
edit: I want to remove the white space before the actual plot and after my text. any ideas?
With the plotrix package:
library(plotrix)
# get an empty box
plot(0:10, type="n", axes=FALSE, xlab=NA, ylab=NA)
# rectangle filled with a gradient
gradient.rect(0, 0, 10, 5, col=smoothColors("red",38,"blue"), border=NA)
# vertical bar
segments(3.5, 0, 3.5, 5, lwd=2)
text(3.5, 0, "0.35", pos=1, xpd=TRUE)
For something in base R, an imperfectly modified version of this solution, might work.
color.bar <- function(lut, min, max=-min, nticks=11, ticks=seq(min, max, len=nticks), title='') {
scale = (length(lut)-1)/(max-min)
dev.new(width=1.75, height=5)
plot(c(min,max), c(0,10), type='n', bty='n', xaxt='n', xlab='', yaxt='n', ylab='', main=title)
for (i in 1:(length(lut)-3)) {
x = (i-1)/scale + min
rect(x, 0 ,1, 30/scale, col=lut[i], border=NA)
}
}
Then make the graph as follows -
color.bar(colorRampPalette(c("light green", "yellow", "orange", "red"))(100), 0, 1)
At this point perhaps you can add an abline(v = 0.35) to get what you want?
You could even try pointing to the appropriate position using an arrow using
arrows(0.35, -1, 0.35, 0, length = 0.07, angle = 25)
I like to use it for prepare the plot, add horizontal lines and finally plot the bars. When I do it like below, R plots every thing twice and this looks not so nice. For a normal plot the equivalent I search is: plot(NULL, xlim=c(1,2), ylim=c(3,5), axes=F)
data = c(1,4,2)
barplot(data)
abline(h=seq(0,5,1), col="red")
barplot(data, add=T)
I prefer to have a base solution.
Plotting with col and border set to NA and axes = FALSE will effectively create an empty plot. Then you can add abline and actual barplot
data = c(1,4,2)
barplot(data, col = NA, border = NA, axes = FALSE)
abline(h=0:5, col="red")
barplot(data, add = TRUE)
I am not entirely sure if this is what you are looking for. You can "plot" with white bar and white boarder line. For example,
data = c(1,4,2)
# "white" / rgb(1,1,1) / rgb(255,255,255, maxColorValue=255) all identical
barplot(data, col=rgb(255, 255, 255, maxColorValue=255), border="white")
abline(h=seq(0,5,1), col="red")
barplot(data, add=T, xaxt="n", yaxt="n")
axis(2, at=c(0:4), label=c(0:4))
axis(1)
but this will produce same plot that is produced by your sample code.
I have this code:
# Plotting everything
plot( p1, col= "lightgreen", xlim=c(-2.5,4.5), ylim=c(0, 700), main="Daily Total Precipitation for AR and Oct-May", xlab="ln(x)" , ylab="Frequency", xaxt = "n") # first histogram
plot( p2, col="red", xlim=c(-2.5,4.5), ylim=c(0, 700), xaxt = "n" , add=T)
# Adding in text labels on top of the bars
text(x, y, paste(round(percents,2),"%"), cex=0.50, pos=3, offset=0.3, col="black")
axis(side=1, at=breaks) # new x-axis
# parameter that needs to be set to add a new graph on top of the other ones
par(new=T)
plot(x, percents, xlim=c(-2.5,4.5), type="l", col="yellow", lwd=3.0, axes=F, ylab=NA, xlab=NA)
axis(side=4, at=seq(0,100,by=10), col="yellow", col.axis="yellow") # additional y-axis
mtext("Percent", side=4, col="yellow")
# legend settings
legend("topleft", c("AR", "Oct-May"), lwd=10, col=c("red", "lightgreen"))
Which produces this graph:
And I can't seem to figure out how to get the secondary y-axis label to show up in the correct position. Any help or suggestions is greatly appreciated.
Edit: Using RStudio.
One option is to specify the line argument to mtext(). In the example below I add a couple more lines to the right (side = 4) margin of the plot using par(), and then I draw three labels using mtext() at the default (line = 0), line 3 (line = 3), and line -3 (line = -3):
op <- par(mar = c(5,4,4,4) + 0.1)
plot(1:10)
mtext("line0", side = 4)
mtext("line3", side = 4, line = 3)
mtext("line-3", side = 4, line = -3)
par(op)
Note that line numbers increase away from the plot region and that negative line values move into the plot region, or to the left of the right boundary of the plot region.
It takes a little playing with the number of margin lines (as set in par(mar = x)) and which line you want to draw on using mtext(), but a little trial and error should get you what you want.
Note also that you don't need to specify integer values for the line argument. You can specify fractions of lines too: line = 2.5.