How to increase y-axis in barplot? - r

My code:
#!/usr/bin/R
layout(matrix(c(1, 1, 2, 2, 3, 3, 4, 4, 5), ncol=1))
# main plots
par(mar=c(5,2,4,2))
fcm <-c(14.0,14.1,13.0,14.2,14.7,13.8,14.0)
gk <-c(12.1,12.5,12.2,12.0,11.5,12.0,11.4)
gg <-c(14.0,14.1,13.3,12.8,12.0,12.2,12.0)
data1 <- rbind(fcm,gk,gg)
colnames(data1) <- c(6,7,8,9,10,11,12)
fcm <-c(2.65,2.55,2.4,2.45,2.45,2.5,2.45)
gk <-c(2.45,2.55,2.4,2.3,2.2,2.35,2.1)
gg <-c(2.6,2.65,2.5,2.35,2.4,2.4,2.2)
data2 <- rbind(fcm,gk,gg)
colnames(data2) <- c(6,7,8,9,10,11,12)
fcm <-c(8.8,6.5,6.6,8.2,8.0,8.4,9.0)
gk <-c(12.7,11.0,11.1,10.5,10.7,10.0,9.5)
gg <-c(2.1,2.1,1.8,2.0,2.0,1.9,1.8)
data3 <- rbind(fcm,gk,gg)
colnames(data3) <- c(6,7,8,9,10,11,12)
fcm <-c(0.47,0.53,0.45,0.39,0.40,0.47,0.48)
gk <-c(0.45,0.51,0.34,0.40,0.42,0.42,0.44)
data4 <- rbind(fcm,gk)
colnames(data4) <- c(6,7,8,9,10,11,12)
barplot(as.matrix(data1),ylim=c(0,15),main="P wave",
xlab="number of clusters", ylab="traveltime rms(ms)",
col=c("red", "black", "green"), beside=TRUE)
barplot(as.matrix(data2),ylim=c(0,3),main="MT",
xlab="number of clusters", ylab="MT functions",
col=c("red", "black", "green"), beside=TRUE)
barplot(as.matrix(data3),ylim=c(0,13),main="XBI",
xlab="number of clusters", ylab="index value",
col=c("red", "black", "green"), beside=TRUE)
barplot(as.matrix(data4),ylim=c(0,0.6),main="NCE",
xlab="number of clusters", ylab="index value",
col=c("red", "black"), beside=TRUE)
par(mai=c(0,0,0,0))
plot.new()
legend(legend = c("fcm","gk","gg"), fill = c( "red", "black", "green"),
"center", horiz=TRUE)
This image will be part of my paper,plan to submit it to a journal.I am afraid editor will complain that values are not visible.How to change this?My goal is that there is clear distinction between all barplots,of course if this is possible.

Stacking your plots is compressing them unnecessarily & creating a lot of wasted white space.
Why not shift to a 2x2 representation?
Basically all you need to change is:
layout(matrix(c(1, 2, 5, 3, 4, 5), ncol=2))
I get this output with par(mar=c(3.1,2.1,2.1,0.1)):
You may notice we're giving too much space to the legend; this can be fixed by tinkering with the heights parameter to layout; here's the output with heights=c(.45,.45,.1). See ?layout.

I prefer #MichaelChirico's answer, but if you need them to be stacked in one column for some reason, here is another option. I've added comments to explain the changes to your original code. Basically, I've created some additional vertical space by playing with the margins and the layout matrix, and getting rid of unnecessary x-axis titles:
# Change layout matrix to allow more vertical space for bottom plot
# (since it is now the only one with an x-axis title)
layout(matrix(1:5, ncol=1), heights=c(rep(0.21,3), 0.26, 0.11))
# main plots
par(mar=c(2,2.5,3.5,2)) # Decrease top and bottom margins
fcm <-c(14.0,14.1,13.0,14.2,14.7,13.8,14.0)
gk <-c(12.1,12.5,12.2,12.0,11.5,12.0,11.4)
gg <-c(14.0,14.1,13.3,12.8,12.0,12.2,12.0)
data1 <- rbind(fcm,gk,gg)
colnames(data1) <- c(6,7,8,9,10,11,12)
fcm <-c(2.65,2.55,2.4,2.45,2.45,2.5,2.45)
gk <-c(2.45,2.55,2.4,2.3,2.2,2.35,2.1)
gg <-c(2.6,2.65,2.5,2.35,2.4,2.4,2.2)
data2 <- rbind(fcm,gk,gg)
colnames(data2) <- c(6,7,8,9,10,11,12)
fcm <-c(8.8,6.5,6.6,8.2,8.0,8.4,9.0)
gk <-c(12.7,11.0,11.1,10.5,10.7,10.0,9.5)
gg <-c(2.1,2.1,1.8,2.0,2.0,1.9,1.8)
data3 <- rbind(fcm,gk,gg)
colnames(data3) <- c(6,7,8,9,10,11,12)
fcm <-c(0.47,0.53,0.45,0.39,0.40,0.47,0.48)
gk <-c(0.45,0.51,0.34,0.40,0.42,0.42,0.44)
data4 <- rbind(fcm,gk)
colnames(data4) <- c(6,7,8,9,10,11,12)
# Remove x-axis title from first three plots
# Add las=1 to all plots, so y-axis labels will be rotated
barplot(as.matrix(data1),ylim=c(0,15),main="P wave",
ylab="traveltime rms(ms)", las=1,
col=c("red", "black", "green"), beside=TRUE)
barplot(as.matrix(data2),ylim=c(0,3),main="MT",
ylab="MT functions", las=1,
col=c("red", "black", "green"), beside=TRUE)
barplot(as.matrix(data3),ylim=c(0,13),main="XBI",
ylab="index value", las=1,
col=c("red", "black", "green"), beside=TRUE)
# Change bottom margin for last plot so x-axis title will be included
par(mar=c(4,2.5,3.5,2))
barplot(as.matrix(data4),ylim=c(0,0.6),main="NCE",
xlab="number of clusters", ylab="index value", las=1,
col=c("red", "black"), beside=TRUE)
par(mai=c(0,0,0,0))
plot.new()
legend(legend = c("fcm","gk","gg"), fill = c( "red", "black", "green"),
"center", horiz=TRUE)

I can't post comment, so I've decided to add it as answer...
Let's try with las and cex parameters:
barplot(as.matrix(data1),ylim=c(0,15),main="P wave",
xlab="number of clusters", ylab="traveltime rms(ms)",
col=c("red", "black", "green"), beside=TRUE, las=1, cex.axis=.9)
You can also enlarge bars area by reducing first and third element of mar paratemer.

Related

Legend not plotting in chart

I need help with the legend, when I run the code, it doesn't appear.
v_Treasury <- c("DGS5", "DGS10", "DGS30")
getSymbols(Symbols = v_Treasury,
src = "FRED")
I am cleaning the dataset from NA
US5Y<-DGS5["2021/2021"]
US5Y<-na.omit(US5Y)
US10Y<-DGS10["2021/2021"]
US10Y<-na.omit(US10Y)
US30Y<-DGS30["2021/2021"]
US30Y<-na.omit(US30Y)
But the plot has no legend in the graph
plot(US5Y, lwd=2, col="blue",lty = "solid" ,ylab="", xlab="",ylim=c(0.4,3),
main = "US Treasury Yields")
lines(US10Y, col="black", lwd=2, lty="dotted")
lines(US30Y, col="red", lwd=2, lty="dashed")
legend(x="topleft", legend=c("US5Y", "US10Y","US30Y"), col=c("blue", "black", "red"),
lwd=2, lty=c("solid", "dotted","dashed"))
I want to know what is wrong with the code, because the legend is not in the graph
Here is a potential solution:
library(quantmod)
# Data from https://fred.stlouisfed.org/series/DGS5
DGS5 <- read.csv("DGS5.csv")
DGS10 <- read.csv("DGS10.csv")
DGS30 <- read.csv("DGS30.csv")
# the "getSymbols" function from quantmod specifies that
# this is not an 'ordinary' plot - it is an xts 'time-series' plot
# this has implications for e.g. the legend
v_Treasury <- c("DGS5", "DGS10", "DGS30")
getSymbols(Symbols = v_Treasury,
src = "FRED")
US5Y<-DGS5["2021/2021"]
US5Y<-na.omit(US5Y)
US10Y<-DGS10["2021/2021"]
US10Y<-na.omit(US10Y)
US30Y<-DGS30["2021/2021"]
US30Y<-na.omit(US30Y)
# If you turn off clipping ("xpd=NA"), you can see where
# "legend()" is trying to put the legend ("topleft")
par(xpd=NA)
plot(US5Y, lwd=2, col="blue",lty = "solid" ,ylab="", xlab="",ylim=c(0.4,3),
main = "US Treasury Yields")
lines(US10Y, col="black", lwd=2, lty="dotted")
lines(US30Y, col="red", lwd=2, lty="dashed")
legend(x = "topleft", y = NULL, legend=c("US5Y", "US10Y","US30Y"),
col=c("blue", "black", "red"),
lwd=2, lty=c("solid", "dotted","dashed"))
# If you use the "addLegend()" function from the xts package
# then your legend goes where you want it to go
par(xpd=TRUE)
plot(US5Y, lwd=2, col="blue",lty = "solid" ,ylab="", xlab="",ylim=c(0.4,3),
main = "US Treasury Yields")
lines(US10Y, col="black", lwd=2, lty="dotted")
lines(US30Y, col="red", lwd=2, lty="dashed")
addLegend(legend.loc = "topleft", legend.names = c("US5Y", "US10Y","US30Y"),
col=c("blue", "black", "red"), fill=c("blue", "black", "red"), ncol = 1)
Here is a different solution (see the legend.loc option of plot.xts).
library(quantmod)
v_Treasury <- c("DGS5", "DGS10", "DGS30")
getSymbols(Symbols = v_Treasury, src = "FRED")
US5Y <- DGS5["2021/2021"]
US5Y <- na.omit(US5Y)
US10Y <- DGS10["2021/2021"]
US10Y <- na.omit(US10Y)
US30Y <- DGS30["2021/2021"]
US30Y <- na.omit(US30Y)
# Merge the 3 xts objects into one
USdat <- merge(US5Y, US10Y, US30Y, all=TRUE)
# Add a legend using the legend.loc option of plot.xts
plot(USdat, lwd=2,
ylab="", xlab="",ylim=c(0.4,3),
col=c("blue", "black", "red"),
lty=c("solid", "dotted","dashed"),
main = "US Treasury Yields", legend.loc="topright")

R plot legend: Reduce space between legend columns

I am using vegan library to make some plots, with this code:
raremax <- min(colSums(mydata))
col <- palette()
lty <- c("solid", "dashed", "longdash", "dotdash")
pars <- expand.grid(col = col, lty = lty, stringsAsFactors = FALSE)
out <- with(pars[1:18, ], rarecurve(mydata, step = 100, sample = raremax,
cex =0.6, ylab="OTUs", label=F, col=col, lty=lty, lwd=2))
Then I add a legend using this code:
legend("bottomright", names(mydata), col=pars[1:18,1], lty= pars[1:18,2],
lwd=2, cex=0.5, xjust=1, ncol=2, x.intersp=0.5, y.intersp=0.5, bg="white")
The resulting graph looks like this:
I would like to reduce the space between legend columns, also reducing the size of the legend box, but I can't find a way to do that.
Anyone could provide me some help?
A combination of the legend() parameters "x.intersp" and "text.width" should be helpful.
Decreasing "x.intersp" (default value = 1, for me 0.25 looked good) should move your the legend labels closer to their respective points. Decreasing "text.width" (default value=NULL, for me 0.045 looked good) moves the columns closer together.

legend in a forest plot

I am having a hard time with the forest plot package in R. Here is my code. Actually everything works well beside the legend.
. However for the legend, I would like to have a Blue Circle, A red Square and a green losange in stead of 3 squares.
Any idea?
Thanks in advance.
Peter
library(forestplot)
test_data <- data.frame(coef1=c(0.54,0.72,0.57),
coef2=c(0.59,0.79,0.58),
coef3=c(0.49,0.60,0.48),
low1=c(0.41,0.46,0.42),
low2=c(0.44,0.49,0.42),
low3=c(0.37,0.37,0.35),
high1=c(0.72,1.12,0.77),
high2=c(0.78,1.26,0.80),
high3=c(0.65,0.99,0.66))
col_no <- grep("coef", colnames(test_data))
row_names <- list(
list("Behavioral CVH","Biological CVH","Total CVH"))
coef <- with(test_data, cbind(coef1, coef2, coef3))
low <- with(test_data, cbind(low1, low2, low3))
high <- with(test_data, cbind(high1, high2, high3))
forestplot(row_names, coef, low, high,
title="Paris Prospective Study 3",
fn.ci_norm=matrix(c("fpDrawCircleCI", "fpDrawNormalCI","fpDrawDiamondCI"),
nrow = 3, ncol=3, byrow=T),
zero = c(1), boxsize=0.05,
col=fpColors(box=c("royalblue", "gold", "black"),
line=c("darkblue", "orange", "black"),
summary=c("darkblue", "red", "black"),
hrz_lines = "#444444"),
xlab="Odds ratio & 95% Confidence intervals",
vertices = TRUE,
new_page = TRUE,
legend=c("Q2 vs. Q1","Q3 vs. Q1","Q4 vs. Q1"),
legend_args = fpLegend(pos = list("topright"),
title="Legend",
r = unit(0, "snpc"),
gp = gpar(col="#CCCCCC", lwd=1.5)))
One thing you can do is use the "regular" call to legend, outside the call for the forestplot.
To do that, you'll first have to call plot.new:
plot.new()
forestplot(...) # without the legend part
legend("topright", c("Q2 vs. Q1","Q3 vs. Q1","Q4 vs. Q1"), title="Legend", border="#CCCCCC", box.lwd=1.5,
col=c("blue", "red", "green"), pch=c(16, 15, 18))

How to set text size in legend?

I have tried this
#!/usr/bin/R
fcm <-c(13.0,12.5,11.8)
gk <-c(10.9 , 10.5 , 10.2)
gg <-c(12.0 , 11.0 , 10.8)
data1 <- rbind(fcm,gk,gg)
colnames(data1) <- c(5,6,7)
barplot(as.matrix(data1),ylim=c(0,15),main="P wave",
xlab="number of clusters",ylab="traveltime rms(ms)",
col=c("red", "black", "green"), beside=TRUE)
op <- par(cex=.64)
legend(legend = c("fcm","gk","gg"), fill = c( "red", "black", "green"),
"top", horiz=TRUE,text.font=3)
par(op)
dev.copy(png,"s1.png",width=4,height=4,units="in",res=200)
dev.off()
When image appears,R graphics device 2 Active looks fine.But save image has overlapping of words in legend.
How to solve this?
Instead of using dev.copy to save your image, you can call png before "drawing" it :
png("Z:/GES - catalogue signatures/biblio/cdf pour Wolf/s1.png", width=4,height=4,units="in",res=200)
barplot(as.matrix(data1),ylim=c(0,15),main="P wave",
xlab="number of clusters",ylab="traveltime rms(ms)",
col=c("red", "black", "green"), beside=TRUE)
op <- par(cex=.64)
legend(legend = c("fcm","gk","gg"), fill = c( "red", "black", "green"),
"top", horiz=TRUE,text.font=3)
par(op)
dev.off()

Pie-chart and legend are overlapping

I'm trying to create a pie-chart, like here, but the legend and the actual pie are overlapping.
The code I used is here:
library(ggplot2)
library(grid)
par(mai = c(1,1,1,1))
numb <- c(41, 30, 21, 8)
colors <- c("black", "grey70", "grey30","white")
numb_labels <- round(numb/sum(numb) * 100, 1)
numb_labels <- paste(numb_labels, "%", sep="")
xx <- c("the process of familiarizing with the codebase",
"the patch review process",
"the impact on the users of the project",
"high degree visibility of the contributions")
pie(numb, col=colors, labels=numb_labels, clockwise=TRUE)
legend("top", legend = xx, fill=colors, bty="n")
Can someone help me?
I don't know why you attach ggplot2.
You can use layout:
par(mai = c(0,0,0,0))
layout(c(1,2),heights=c(0.3,1))
plot.new()
legend("bottom", legend = xx, fill=colors, bty="n")
pie(numb, col=colors, labels=numb_labels, clockwise=TRUE)
One solution is to use argument xpd=TRUE with function par() and then set legend outside the plot region with coordinates of legend.
par(xpd=TRUE)
pie(numb, col=colors, labels=numb_labels, clockwise=TRUE)
legend(-1.4,1.6, legend = xx, fill=colors, bty="n")
par(mai = c(1,1,4,1), xpd=TRUE)
pie(numb, col=colors, labels=numb_labels, clockwise=TRUE)
legend("top", inset=c(0,-0.5),cex=0.9,legend = xx, fill=colors, bty="n")
Note: First element of inset shift your legend horizontally and the second one vertically.
cex parameter can resize your legend. So may adjust your legend by changing those parameter values.

Resources