This question already has an answer here:
Show element values in barplot
(1 answer)
Closed 5 years ago.
i want to add label for each bar with Y value
You can add y values to barplot() with text()attribute on a new line like follows in this example:
library(tibble)
df <- data_frame(month=month.abb[1:12], Freq=c(12, 11, -5, 3, 4, 1, 43, 5,60,34,-4,3))
p<-barplot(df$Freq, col = rainbow(20), ylim=c(-10,60), names.arg = df$month, space=0, axes=F)
xval = seq(-5, 60, 5)
axis(side = 2, at = xval, labels = FALSE, xpd=T)
axis(side = 2, at = xval, tick = FALSE, labels = xval, xpd=T)
text(p, df$Freq + 2*sign(df$Freq), labels=df$Freq, xpd=TRUE) #this is the line of your interest in regards to the value labels
Related
Need to move rotated labels on the top of the bar using barplot. What parameter am I missing here?
code:
df<- data.frame(a=strrep(letters[1:20], 10) , b=runif(20, min=1, max=30))
df<- df[order(df$b, decreasing = TRUE),]
row.names(df)<- df$a
par(mar=c(15, 5, 3, 2)+ 0.2)
x<- barplot(df$b, c(2, 4, 1, 6), ylim = c(0, 30), ylab="statistics", col = heat.colors(20), xaxt="n")
label = row.names(df)
text(cex=0.2, x=x, y=-1.25, label, xpd=TRUE, srt=45)
You need to change the position of y-axis in text with a suitable offset. (I have used + 1 here).
x <- barplot(df$b, c(2, 4, 1, 6), ylim = c(0, 30), ylab="statistics",
col = heat.colors(20), xaxt="n")
label = row.names(df)
text(cex=0.2, x=x, y=df$b + 1, label, xpd=TRUE, srt=45)
I want to create a simple one-dimensional plot in R ranging from 0 to ten, with a scale (small lines for integer values 1,2, etc.), a slightly higher line for 5 (the median) and slightly higher than all of the other for 0 and 10. Then I want to fill this plot with a few points representing values like 2, 4, 5, 6, 8, and a text above each one of them with corresponding labels (like "party voted", "closest party", "individual 1", "expert", "individual 2"). It can be smaller labels, like "PV", "CP", etc.
I would like to have control over shape and color (say in data-points 4 and 6 I have a circle filled in black but in position 2 I have a not filled square, in position 5 I have a green circle filled-in, and in position 8 I have a black triangle, also filled). I would like to have 0, 10 and 5 marked in the labels as well.
A very rough representation of what I am trying to draw is in the image below (it has all the elements I want, at least).
In this Stack Overflow question there is some code on one-dimension plot, I have tried to adapt it to what I need but didn't get to it
I've assumed from the link in the question that you are looking for a base R solution.
There may be more efficient solutions but this seems to get you where you want.
I've avoided the need for arrows by forcing the labels to run over two lines and reducing the text size on the plot so they do not overlap.
You could manage this with arrows if need be, but this seems it will need a lot of extra code.
# data
df <- data.frame(desc = c("Party voted", "Closest party", "Individual 1", "Expert", "Individual 2"),
score = c(2, 4, 5, 6, 8),
y = 1)
# add line break to labels
df$desc <- gsub("\\s", "\n", df$desc)
plot(df$score,
df$y,
# type = "o",
xlim = c(0, 10),
pch = c(1, 21,21,21, 24),
col = c("black", "black", "green", "black", "black"),
bg = c("black", "black", "green", "black", "black"),
cex = 1.5,
xaxt = "n", #remove tick marks
yaxt = "n",
ylab = '', # remove axis labels
xlab = '',
bty = "n") # remove bounding box
axis(side = 1,
0:10,
pos = df$y,
labels = FALSE,
tck = 0.02)
axis(side = 1,
0:10,
pos = df$y,
labels = c(0, rep("", 4), 5, rep("", 4), 10),
tck = -0.02)
axis(side = 1,
c(0, 5, 10),
pos = df$y,
labels = FALSE,
tck = 0.05)
axis(side = 1,
c(0, 5, 10),
pos = df$y,
labels = FALSE,
tck = -0.05)
text(x = df$score,
y = df$y,
labels = df$desc,
pos = 3,
offset = 1,
cex = 0.75)
Created on 2021-04-28 by the reprex package (v2.0.0)
I'm trying to make a scatterplot that shows the age of people on the y-axis and the way they have been positioned on the x-axis (either 0° or 15° elevated).
My dataset is called raw.
I have used the function plot(raw$position, raw$age). Instead of just showing 0 and 15, the x-axis gives out 0, 5, 10, 15 (with no dots for 5 or 10, since the only two positionings are 0 and 15°).
Is there a way to get it to only show my 0 and 15 on the x-axis?
As you didn't supply the original data below is a reproducible example with my own. There's two parts to doing this:
Include yaxt = "n" in plot to suppress the original y-axis in the plot
Use axis(2, labels = c(0, 15), at = c(0, 15)) to set the y-axis (side = 2) with labels (labels) called c(0,15) at the points (at) on the axis (0,15).
set.seed(1)
df = data.frame(
age = round(runif(10, 20, 30)),
position = rbinom(10, 1, 0.5)*15
)
plot(df$age, df$position, yaxt = "n")
axis(2, labels = c(0, 15), at = c(0, 15))
Edit: Just re-read your question and saw you want to edit the x-axis, which you do with this:
Same as above but now set xaxt = "n" and side = 1
set.seed(1)
df = data.frame(
age = round(runif(10, 20, 30)),
position = rbinom(10, 1, 0.5)*15
)
plot(df$position, df$age, xaxt = "n")
axis(1, labels = c(0, 15), at = c(0, 15))
I'm wondering how I could add another row of x-axis values right below (along side) the current x-axis values (i.e., 0 to 1) that begin from (ie., 0% to 100%)?
To summarize in 3 steps:
(A): the second row of axis values need NOT have tick marks. (B):, the second row of axis values need to appear exactly below each corresponding value of the first x-axis values. (C):, the second row of axis values must show a "%" sign next to them.
plot(1, ty='l', ann = F, axes = F, xlim = c(0, 1) )
axis(1, at = round(seq(0, 1, len = 9), 2), font = 2, cex.axis = 1.2 )
Look up pos, tck, and tick of axis
par(mar = c(10,3,3,3))
plot(1, 1, type = 'l', ann = F, xlim = c(0, 1), xaxt = "n")
axis(1, at = round(seq(0, 1, len = 9), 2), font = 2, cex.axis = 1.2 ) #First axis
axis(1, at = round(seq(0, 1, len = 9), 2),
labels = paste(100* round(seq(0, 1, len = 9), 2),"%",sep=""),
pos = par("usr")[3] - 1 * 0.1 * (par("usr")[4] - par("usr")[3]),
tick = FALSE, font = 2, cex.axis = 1.2) #2nd axis labels
I don't know how to change the code so that the y-axis in the barPlot shows completely? I expect it to show up to 10 as I have a 9.2 in my data points but it shows only up to 8. Any idea what's the hack to this?
Here's the code:
And here's what it shows:
just set the ylim = c(0, 10) like the way you changed the xlim
Whatever the axis that the groups go on isn't drawn by default, so a vertical bar plot won't have x-axis; horizontal won't have y-axis. You can add that of course. Use the return value of barplot:
par(mfrow = c(2, 1))
bp <- barplot(c(8, 5), width = .5, main = 'Feature Exploration', xlim = c(0,4), ylim = c(0, 10),
ylab = 'Errors (%)', xlab = 'ML Models', col = c('gray27','orangered4'))
## this will draw the x-axis but at points 1, 2, 3, ... which is not
## where the centers of your bars are plotted; you get that info in bp
axis(1)
bp <- barplot(c(8, 5), width = .5, main = 'Feature Exploration', xlim = c(0,4), ylim = c(0, 10),
ylab = 'Errors (%)', xlab = 'ML Models', col = c('gray27','orangered4'))
## so try again with a fancy axis, bp is a matrix containing the centers
## of the plotted bars
axis(1, at = bp, labels = c('Model1','Model2'), lwd = 0, lwd.ticks = 1, tcl = -.5)