Related
df <-data.frame(y=c(69,61,61,78,69,66,68,59,59,75,67,67,69,61,63,77,67,67,68,61,61,76,66,64), x=gl(4,6))
bx.p <- boxplot(y~x, df,main="Accuracy",ylab="Accuracy(%)",xlab="Models",xlim=c(0.5,4.5),ylim=c(55,90),boxfill=0,medcol=2,boxwex=0.4,names=c("a","b","c","d") )
bx.p$stats[3, ] <- unclass(with(df, by(y, x, FUN = mean)))
bxp(bx.p, add=T, boxfill="transparent", medcol="blue", boxwex=0.4,axes=F, outpch = NA, outlty="blank", boxlty="blank", whisklty="blank", staplelty="blank")
legend(x=3.8,y=90.5, lty=c(1, 1), lwd=rep(3, 2), col=c("red", "blue"), box.lwd=0.2,legend = c("median", "mean"), cex=0.8,horiz = FALSE, bg="transparent")
grid(nx=13, ny=13)
add=TRUE is not applied
It doesn't change although i add (add=TRUE)
bx.p <- boxplot(y~x, df,main="Accuracy",ylab="Accuracy(%)",xlab="Models",xlim=c(0.5,4.5),ylim=c(55,90),boxfill=0,medcol=2,boxwex=0.4,names=c("a","b","c","d"),add=TRUE )
You can add them manually if you like by using the segments() function in base R plots:
df <-data.frame(y=c(69,61,61,78,69,66,68,59,59,75,67,67,69,61,63,77,67,67,68,61,61,76,66,64), x=gl(4,6))
plot(x = c(50,90), y = c(0,10), xlab = NA, ylab = NA, axes = FALSE, type = "n")
bx.p <- boxplot(y ~ x, df, main="Accuracy",
ylab = "Accuracy(%)", xlab = "Models",
xlim = c(0.5,4.5), ylim = c(55,90),
boxfill = "white", medcol = 2, boxwex = 0.4,
names = c("a","b","c","d") )
bx.p$stats[3, ] <- unclass(with(df, by(y, x, FUN = mean)))
bxp(bx.p, add=T, boxfill="transparent", medcol="blue", boxwex=0.4,
axes=F, outpch = NA, outlty="blank", boxlty="blank", whisklty="blank", staplelty="blank")
legend(x=3.8,y=90.5, lty=c(1, 1), lwd=rep(3, 2), col=c("red", "blue"), box.lwd=0.2,legend = c("median", "mean"), cex=0.8,horiz = FALSE, bg="transparent")
segments(x0 = seq(0.5, 10, 0.5), y0 = 50, y1 = 100, lty = 2, lwd = 0.75, col = "lightgrey")
segments(x0 = 0, x1 = 10, y0 = seq(50, 100, 5), lty = 2, lwd = 0.75, col = "lightgrey")
I can plot the barplot in Excel with decimal points in y-axis limits as shown below.
But I would not be able to change the y-axis limits in R.
Here is my code in R.
par(mfrow=c(1, 1), mar=c(7, 4, 5, 6))
mydata <- data.frame(Algorithm1=c(95.85, 96.94), Algorithm2=c(96.04, 96.84), Algorithm3=c(95, 95.30))
barplot(as.matrix(mydata), main="Precision", ylim=range(0:100),
beside=T, col=c("red", "blue"), las=1, border = 0, cex.lab=1, cex.axis=1, font=1,col.axis="black", ylab = "Percentage",
legend.text = c("X1", "X2"),
args.legend = list(x ='topright', bty='n', inset=c(-0.20,0)))
Thanks in advance for your answer.
You can also use ggplot2 and scales.
library(dplyr)
library(ggplot2)
library(scales)
mydata <- data.frame(Algorithm = rep(c('Algorithm1','Algorithm2','Algorithm3'), each=2),
variable_x = rep(c('X1','X2'),3),
values=c(0.9585, 0.9694,0.9604, 0.9684, 0.95, 0.9530))
mydata %>%
ggplot(aes(x=Algorithm,y=values,fill=variable_x))+
geom_bar(stat='identity', position='dodge')+
scale_y_continuous(labels = scales::percent, limits = c(0.94,0.975), oob = rescale_none)+
scale_fill_manual(values= c(X1='red',X2='blue'))
Set the limit of y and xpd = FALSE.
FALSE : all plotting is clipped to the plot region
TRUE : all plotting is clipped to the figure region
NA : all plotting is clipped to the device region
library(RColorBrewer)
color <- brewer.pal(3, "Set1")[2:1]
plot.new()
plot.window(xlim = c(0, 10), ylim = c(94, 97.5), yaxs = "i")
abline(h = seq(94, 97.5, 0.5), col = 8)
barplot(as.matrix(mydata), beside = T, col = color,
border = NA, legend.text = c("X1", "X2"),
args.legend = list(x = 'topright', bty = "n"), xpd = F, add = T)
You could do:
tickPoints <- 20 * (0:5)
par(mfrow = c(1, 1), mar = c(7, 4, 5, 6))
mydata <- data.frame(
Algorithm1 = c(95.85, 96.94),
Algorithm2 = c(96.04, 96.84),
Algorithm3 = c(95, 95.30)
)
barplot(
as.matrix(mydata), main = "Precision", beside = T, col = c("red", "blue"),
las = 1, border = 0, cex.lab = 1, cex.axis = 1, font = 1, col.axis = "black",
ylab = "Percentage", legend.text = c("X1", "X2"),
args.legend = list(x = 'topright', bty = 'n',inset = c(-0.20, 0)),
axes = FALSE,
ylim = range(tickPoints)
)
axis(side = 2, at = tickPoints, labels = sprintf("%0.1f", tickPoints))
Note the axes = FALSE in the call to barplot
Any tips to remove the zero labels in between the histogram bars?
hist(links$Survey_Duration, breaks = seq(0,50,5), main = "Survey Duration",
labels = TRUE, border = "black",
xlab = "Survey", ylim = c(0, 15), col = "gray", las = 1, xaxt='n')
axis(side=1, at=seq(0,50,5), labels=seq(0,50,5))
abline(v = mean(links$Survey_Duration), col = "royalblue", lwd = 1.5)
abline(v = median(links$Survey_Duration), col = "red", lwd = 1.5)
legend(x = "topright", c("Mean", "Median"), col = c("royalblue","red"),
lwd = c(1.5,1.5))
How about this?
# modify data so there's zero in one of the bins
mtcars$mpg <- ifelse(mtcars$mpg >= 25 & mtcars$mpg <= 30, NA, mtcars$mpg)
# save plot parameters
h <- hist(mtcars$mpg, plot = FALSE)
# produce plot
plot(h, ylim = c(0, 14))
# add labels manually, recoding zeros to nothing
text(h$mids, h$counts + 1, ifelse(h$counts == 0, "", h$counts))
A slightly different answer using the labeling in hist instead of adding text afterwards.
You do not provide your data, so I will use some data that is handy to illustrate.
The labels argument can specify the individual labels
H1 = hist(iris$Sepal.Length, breaks = 3:8, plot=FALSE)
BarLabels = H1$counts
BarLabels[BarLabels == 0] = ""
hist(iris$Sepal.Length, breaks = 3:8, labels = BarLabels)
Thanks #Daniel Anderson, it Ok now (Thumbs Up)
links$Survey_Duration <- ifelse(links$Survey_Duration > 15 &
links$Survey_Duration <= 25,
NA,
links$Survey_Duration)
h <- hist(links$Survey_Duration, breaks = seq(0,50,5), plot = FALSE)
plot(h, ylim = c(0, 14), main = "Survey Duration", xlab = "Time", col = "gray", las = 1)
text(h$mids, h$counts + 1, ifelse(h$counts == 0, "", h$counts))
axis(side=1, at=seq(0,50,5), labels=seq(0,50,5))
abline(v = mean(links$Survey_Duration), col = "royalblue", lwd = 1.5)
abline(v = median(links$Survey_Duration), col = "red", lwd = 1.5)
legend(x = "topright",
c("Mean", "Median"),
col = c("royalblue","red"),
lwd = c(1.5,1.5))
I want to display two plots with the same x-values above each other. But the plots don't align.
How can I align them?
Code:
dat <- data.frame(d = LETTERS[1:5], c = c(39, 371, 389, 378, 790), r = c(39,
332, 18, -11, 412))
par(mfrow=c(2,1))
plot(dat$c, type = "s", ylim = c(0, max(dat$c)), xlab = "", ylab = "", axes = FALSE, col = "#4572a7", lwd = 2)
axis(1, at = c(1:length(dat$c)), labels = dat$d, lty = 0)
axis(2, lty = 0, las = 1)
barplot(dat$r, names.arg = dat$d, col = "#008000", border = NA, axes = FALSE)
axis(2, lty = 0, las = 1)
abline(h = 0, col = "#bbbbbb")
We need to get the x-coordinates of the center of each bar and use those coordinates as the x-values of the first plot. We also need to set the same xlim values for each plot:
# Get x coordinates of center of each bar
pr = barplot(dat$r, names.arg = dat$d, col = "#008000", border = NA, axes = FALSE,
plot=FALSE)
par(mfrow=c(2,1))
# Apply the x coordinates we just calculated to both graphs and give both
# graphs the same xlim values
plot(pr, dat$c, type = "s", ylim = c(0, max(dat$c)), xlab = "", ylab = "", axes = FALSE,
col = "#4572a7", lwd = 2, xlim=range(pr) + c(-0.5,0.5))
axis(1, at = pr, labels = dat$d, lty = 0)
axis(2, lty = 0, las = 1)
barplot(dat$r, names.arg = dat$d, col = "#008000", border = NA, axes = FALSE,
xlim=range(pr) + c(-0.5,0.5))
axis(2, lty = 0, las = 1)
I am trying to design a fairly customized plot in R.
One thing I want to do is add tick marks that are different from the labels - that is, only every 5th tick mark will be labeled. I didn't see an easy way to do this, so I did this:
plot(x = Freq_, y = Mean_ipsi,
pch = 20,
ylim = c(-0.5, .9),
col = 1 + (ProbF < .05) + (ProbF < .01),
xaxt = 'n',
xlab = "Frequency (MHz)", ylab = "z-in minus z-out",
main = "Temporal, Engle 1, Epi, subjectwise",
yaxt = 'n')
mtext(text = seq(1.56, 35.1, by = 1.95),
side = 1, at = seq(1.56, 35.1, by = 1.95), cex = .5,line = 0.25)
axis(1, at = Freq_, tick = TRUE, labels = NA)
and it worked just as I wanted.
But when I changed some of the code preceding mtext I got unexpected results
plot(x = Freq_, y = Mean_ipsi,
pch = 20,
ylim = c(-0.5, .9),
col = "red",
xlab = "Frequency (MHz)", ylab = "z-in minus z-out",
main = "Temporal, Engle 1, Epi, subjectwise
\n p values for difference between ipsi and contra",
yaxt = 'n', type = 'o')
mtext(text = seq(1.56, 35.1, by = 1.95),
side = 1, at = seq(1.56, 35.1, by = 1.95),
cex = .5,line = 0.25)
axis(1, at = Freq_, tick = TRUE, labels = NA)
Now, in addition to the x-axis being labeled with every numbers 1.56, 3.51, etc. I get large numbers (cex = 1, I think) at 5, 10 and so on. I don't want these.
I have no idea what is happening here.
You're missing xaxt="n" in your second version.
Freq_ <- seq(1.56, 35.1, by = 1.95)
Mean_ipsi <- (0.01 * Freq_)
ProbF <- 0.0
#First Version
plot(x = Freq_, y = Mean_ipsi,
pch = 20,
ylim = c(-0.5, .9),
col = 1 + (ProbF < .05) + (ProbF < .01),
xaxt = 'n',
xlab = "Frequency (MHz)", ylab = "z-in minus z-out",
main = "Temporal, Engle 1, Epi, subjectwise",
yaxt = 'n')
mtext(text = seq(1.56, 35.1, by = 1.95),
side = 1, at = seq(1.56, 35.1, by = 1.95), cex = .5,line = 0.25)
axis(1, at = Freq_, tick = TRUE, labels = NA)
#=============================================
#Second Version
plot(x = Freq_, y = Mean_ipsi,
pch = 20,
ylim = c(-0.5, .9),
col = "red",
xlab = "Frequency (MHz)", ylab = "z-in minus z-out",
main = "Temporal, Engle 1, Epi, subjectwise
\n p values for difference between ipsi and contra",
yaxt = 'n', type = 'o') ##### <----- add xaxt="n" here #####
mtext(text = seq(1.56, 35.1, by = 1.95),
side = 1, at = seq(1.56, 35.1, by = 1.95),
cex = .5,line = 0.25)
axis(1, at = Freq_, tick = TRUE, labels = NA)