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")
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))
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.