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
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 have two data sets showing the lengths of fish and would like to create side by side histogram plots to compare the data. The issue I'm having is scaling the y-axis and bin sizes so that they are comparable. Instead of counts, I wanted to use %frequency of the data.
I'm also having issues with plotting them side by side when they're coming from two different sources. Can you use the facet_grid or facet_wrap to do this?
Any help would be much appreciated!
EDIT
I used this code which just gives a basic histogram with the counts..
ggplot(snook, aes(sl)) +geom_histogram(binwidth = 20, color="black", fill= "light blue")+
ggtitle("All Snook")+
labs(x="Standard Length(mm)", y="Counts")+
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
Below are the results from using the code offered by SimeonL below
opar <- par(mfrow = c(1,2))
hist(snook$sl, breaks = seq(0, 1000, length = 50), freq = T, main = "All Snook", xlab = "Length (mm)", ylim = c(0, 50), las = 1)
hist(gut_Snook$SL, breaks = seq(0, 1000, length = 50), freq = T, main = "Culled Snook", xlab = "Length (mm)", ylim = c(0, 50), las = 1)
par(opar)
This is close, however it looks like it's still using the counts for the y-axis rather than % frequency.
Two options in base R:
using hist and change y-axis labels to match percentage:
set.seed(23)
df1 <- data.frame(f_size = rnorm(120, 20, 15))
x.1 <- approxfun(c(0, 100), c(0, nrow(df1)))
df2 <- data.frame(f_size = rnorm(70, 5, 5))
x.2 <- approxfun(c(0, 100), c(0, nrow(df2)))
opar <- par(mfrow = c(1,2))
hist(df1$f_size, breaks = seq(-20, 70, length = 40), freq = T, main = "", xlab = "df1_size",
ylim = x.1(c(0, 25)), las = 1, yaxt = "n", ylab = "% Cases")
axis(2, at = x.1(seq(0, 25, 5)), labels = seq(0, 25, 5), las = 1)
hist(df2$f_size, breaks = seq(-20, 70, length = 40), freq = T, main = "", xlab = "df2_size",
ylim = x.2(c(0, 25)), las = 1, yaxt = "n", ylab = "")
axis(2, at = x.2(seq(0, 25, 5)), labels = seq(0, 25, 5), las = 1)
par(opar)
Calculate percentage first and use barplot:
breaks <- seq(-20, 70, length = 40)
df1.perc <- aggregate(df1$f_size, by = list(cut(df1$f_size, breaks, labels = F)), FUN = function(x) (length(x)/nrow(df1))*100)
df2.perc <- aggregate(df2$f_size, by = list(cut(df2$f_size, breaks, labels = F)), FUN = function(x) (length(x)/nrow(df2))*100)
opar <- par(mfrow = c(1,2))
bp <- barplot(height = merge(data.frame(Group.1 = 1:length(breaks)), df1.perc, all.x = T)$x,
xlab = "df1_size", ylab = "% Cases", ylim = c(0, 25), las = 1)
axis(1, at = approx(breaks, bp, xout = seq(-40, 70, by = 10))$y, labels = seq(-40, 70, by = 10))
bp <- barplot(height = merge(data.frame(Group.1 = 1:length(breaks)), df2.perc, all.x = T)$x,
xlab = "df1_size", ylab = "", ylim = c(0, 25), las = 1)
axis(1, at = approx(breaks, bp, xout = seq(-40, 70, by = 10))$y, labels = seq(-40, 70, by = 10))
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))
Hi can anyone see what is the problem with the code below?
output$zscore_chart <- renderPlot({
xvals <- 2:186
req(input$countrySelectZScore)
idx_country = which(input$countrySelectZScore == esiCountries)
max_comp_z <- max(esiData_DF[idx_country, 2:186], na.rm = TRUE)
overall_max_z <- max(max_comp_z, na.rm = TRUE)
foo = ts(esiData_DF[idx_country, 2:186], frequency = 12, start = 2001)
dates = seq(as.Date("2001-01-01"), by = "month", along = foo)
plot(x = 2:186, y = esiData_DF[idx_country, 2:186], type = "l",
xlab = "", ylab = "", col = "grey20", ylim = c(-2, overall_max_z),
lwd=3,las=2)
mtext("Quarterly percentage changes", side = 3, adj = 0, line = 0.1,
cex = 1, font = 0.5)
axis(1, at = xvals, label = dates, cex.axis = 1, las = 2)
mtext("Economic Sentiment Indicators", side = 3, adj = 0,
line = 1.2, cex = 2, font = 2)
legend(
"bottom",
lty = c(1,1),
lwd = c(3,3),
col = c("grey20", "green2"),
legend = c("Economic Sentiment Indicator", "GDP growth"),
bty = "n",
xjust = 0.5,
yjust = 0.5,
horiz = TRUE
)
}, height = 525)
esiData_DF is the DF used to index and plot the correct data. The dataframe has the country names down the left hand side with the dates, monthly across the top. I need the plot to render but it wont when I run the app. Any ideas?
The data continues to the right, up to May 2017 monthly.
Does anyone know how to extract the 'y' off the y-axis while preserving the variable names in the following plot:
par(mar = c(5,7,4,2) +.01)
matrix <- matrix(rnorm(100) ,ncol = 2, nrow =6)
y <- 1:6
par(mar = c(5,7,4,2) +.01)
plot(matrix[,1], y, cex = .8, pch = 20, xlab = "Standardized Mean Differences", col = "darkblue", main = "Balance Assessment", yaxt = "n")
points(matrix[,2], y, cex = .8, pch = 20, col ="cyan")
abline(v = 0, col = "gray50", lty =2)
text(y =1:6, par("usr")[1], labels = c("Var1", "Var2", "Var3", "Var4", "Var5", "Var6"), pos = 2, xpd = TRUE, srt = 0, cex = .8, font = 1, col = "blue")
It's minor, but it's driving me crazy. Thanks!
Just set ylab='' to remove it.