In R, is there a way to add means to a plot at specific x-axis. For example, I want to do something like:
plot(1, 1, xlim = c(0, 6.5), ylim = c(0,300), type = 'n', xlab = '', ylab = '', xaxt = 'n') #xaxs="i",yaxs="i")
boxplot(dataset,at = 0.5, add = T, range = 6,yaxt = 'n')
points(mean(dataset), at = 0.5, add = T)
I get a message saying "at" and "add" are 'not a graphical parameter'. Is there a workaround?
So I am using RStudio. There are six different values (data_a, data_b, data_c, data_d, data_e, and data_f) each with 11 numbers. My current code looks like follows:
par(xpd = FALSE)
par(mar=c(8,4.5,2,1))
plot(1, 1, xlim = c(0, 6.5), ylim = c(0,300), type = 'n', xlab = '', ylab = '', xaxt = 'n') #xaxs="i",yaxs="i")
boxplot(data_a,at = 0.5, add = T, range = 6,yaxt = 'n')
boxplot(data_b,at = 1.5, add = T, range = 6,yaxt = 'n')
boxplot(data_c,at = 2.5, add = T, range = 6,yaxt = 'n')
boxplot(data_d,at = 4, add = T, range = 6,yaxt = 'n')
boxplot(data_e,at = 5, add = T, range = 6,yaxt = 'n')
boxplot(data_f,at = 6, add = T, range = 6,yaxt = 'n')
axis(2, at = 150, pos = -0.65, tck = 0, labels = 'X axis label',cex.axis=1.1)
axis(1, at = c(0.5,1.5,2.5,4,5,6),labels=c('','','','','',''))
axis(1, at = c(1.5,5),pos= -25,labels=c('label 1','labe 2'),tick=FALSE)
axis(1, at = c(3.25),labels=c(''),tck=-0.15)
axis(1, at = c(3.25),pos = -50,labels=c('Y axis label'),tick=FALSE)
abline(v=3.25)
par(xpd = NA)
text(0.5,-30, expression("a))
text(1.5,-30, expression("b"))
text(2.5,-30,"c")
text(4,-30, expression(d))
text(5,-30, expression("e"))
text(6,-30,"f")
Now I want to be able to add mean.
You can try this to get the desired plot (with randomly generated data_a, data_b etc.):
par(xpd = FALSE)
par(mar=c(8,4.5,2,1))
plot(1, 1, xlim = c(0, 6.5), ylim = c(0,300), type = 'n', xlab = '', ylab = '', xaxt = 'n') #xaxs="i",yaxs="i")
data_a <- runif(11, 0, 300)
data_b <- runif(11, 0, 300)
data_c <- runif(11, 0, 300)
data_d <- runif(11, 0, 300)
data_e <- runif(11, 0, 300)
data_f <- runif(11, 0, 300)
boxplot(data_a,at = 0.5, add = T, range = 6,yaxt = 'n')
boxplot(data_b,at = 1.5, add = T, range = 6,yaxt = 'n')
boxplot(data_c,at = 2.5, add = T, range = 6,yaxt = 'n')
boxplot(data_d,at = 4, add = T, range = 6,yaxt = 'n')
boxplot(data_e,at = 5, add = T, range = 6,yaxt = 'n')
boxplot(data_f,at = 6, add = T, range = 6,yaxt = 'n')
axis(2, at = 150, pos = -0.65, tck = 0, labels = 'X axis label',cex.axis=1.1)
axis(1, at = c(0.5,1.5,2.5,4,5,6),labels=c('','','','','',''))
axis(1, at = c(1.5,5),pos= -25,labels=c('label 1','label 2'),tick=FALSE)
axis(1, at = c(3.25),labels=c(''),tck=-0.15)
axis(1, at = c(3.25),pos = -50,labels=c('Y axis label'),tick=FALSE)
abline(v=3.25)
par(xpd = NA)
text(0.5,-30, expression("a"))
text(1.5,-30, expression("b"))
text(2.5,-30,"c")
text(4,-30, expression("d"))
text(5,-30, expression("e"))
text(6,-30,"f")
points(c(0.5,1.5,2.5,4,5,6), c(mean(data_a), mean(data_b), mean(data_c), mean(data_d), mean(data_e), mean(data_f)), pch = 22, col = "darkgrey", lwd = 7)
With boxplots, the location of the plots on the x axis are at the units (1, 2, 3, etc.). You can check this using the function locator().
Here is an example to add means as a red circle to a boxplot using the iris dataset:
boxplot(Sepal.Length ~ Species, data = iris)
points(seq_along(levels(iris$Species)), with(iris, tapply(Sepal.Length, Species, mean)), col = "red")
Related
I have found multiple ways to create a secondary y-axis in plot but I couldn't find a way to create a secondary y-axis in histogram.
Here is a sample code:
a <- sample(90:110, 50, replace=TRUE)
b <- runif(50, min=0, max=1)
hist(a)
lines(b)
b is too small to show in hist(a) so is there any way that I can see both in the histogram?
Technically a solution may be quite an identical to the approach proposed for the plots in this answer. The idea is to use overlapping of two plots as proposed by #r2evans.
It makes sense to use color coding:
# set color rules
col_a <- "red"
col_b <- "darkblue"
col_common <- "black"
Then let's draw the histogram and the plot:
# draw a histogram first
par(mar = c(5, 5, 5, 5) + 0.3)
hist(a, col = col_a, axes = FALSE, xlab = "", ylab = "", main = "")
# add both axes with the labels
axis(side = 1, xlim = seq(along.with = b), col = col_a, col.axis = col_a)
mtext(side = 1, text = "a_value", col = col_a, line = 2.5)
axis(side = 2, col = col_a, col.axis = col_a, ylab = "")
mtext(side = 2, text = "a_Frequency", col = col_a, line = 2.5)
# ... and add an overlaying plot
par(new=TRUE)
plot(b, ylim = c(0, 1), axes = FALSE, col = col_b, type = "l", xlab = "", ylab = "")
points(b, col = col_b, pch = 20, xlab = "", ylab = "")
axis(side = 3, xlim = seq(along.with = b), col = col_b, col.axis = col_b)
mtext(side = 3, text = "b_index", col = col_b, line = 2.5)
axis(side = 4, ylim = c(0, 1), col = col_b, col.axis = col_b)
mtext(side = 4, text = "b_value", col = col_b, line = 2.5)
box(col = col_common)
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.
I was wondering how I can increase the font size of ONLY number "6" in my x-axis in the plot below? (Please see my R code below)
Here is my R code:
plot(1:10, ty = 'n', ann = F, xaxt = 'n', yaxt = 'n')
axis(side = 1, at = 1:10, font = 2)
Another solution
plot(1:10, ty = 'n', ann = F, xaxt = 'n', yaxt = 'n')
axis(1, 1:10, labels = NA)
mtext(text = "6", side = 1, line = 1, at = 6, cex = 3, font = 2, padj = 0.5)
mtext(text = paste(c(1:5,7:10)), side = 1, line = 1,
at = c(1:5,7:10), cex = 1, font = 2, padj = 0.5)
You need to use cex.axis in another axis call exclusively for number 6.
plot(1:10, ty = 'n', ann = F, xaxt = 'n', yaxt = 'n')
axis(side = 1, at = c(1:5,7:10), font = 2)
axis(side = 1, at = 6, , font = 2, cex.axis=2)
EDIT To move the 6 lower as requested, use line:
plot(1:10, ty = 'n', ann = F, xaxt = 'n', yaxt = 'n')
axis(side = 1, at = c(1:5,7:10), font = 2)
axis(side=1, at = 6, labels=FALSE,tick=TRUE) #draw normal tick, no label
axis(side = 1, at = 6, , font = 2, cex.axis=2, line=0.5,tick = FALSE)
I want to have my titles be above my points in a pdf plot. It seems R thinks my title is wider than my plot area even though I have my right and left plot margins set to zero.
x <- seq(from = 0.1, to = 0.9, by = 0.1)
xtitle <- c("0.1","0.2","0.3","0.4","0.5","0.6","0.7","0.8","0.9")
pdf("testpdfopts.pdf", width = 16)
par(mar = c(0, 0, 1, 0)) # only need margin at top for title
plot(
x,
rep(0.9,length(x)),
xlim = c(0, 1),
ylim = c(0, 1),
cex = 1,
xaxt = "n",
yaxt = "n",
xlab = "",
ylab = ""
)
for (i in 1:length(x)) {
title(xtitle[i],adj=x[i],cex.main=1)
}
dev.off()
Thanks in advance for any assistance
I am sort of confused with what you are trying to do. Maybe I should put my comment into complete code:
x <- seq(from = 0.1, to = 0.9, by = 0.1)
plot(
x,
rep(0.9,length(x)),
xlim = c(0, 1),
ylim = c(0, 1),
cex = 1,
xaxt = "n",
yaxt = "n",
xlab = "",
ylab = ""
)
axis(3, at = 1:9/10)
Is this equivalent to what you aim to achieve?
You can get character labels too:
x <- seq(from = 0.1, to = 0.9, by = 0.1)
plot(
x,
rep(0.9,length(x)),
xlim = c(0, 1),
ylim = c(0, 1),
cex = 1,
xaxt = "n",
yaxt = "n",
xlab = "",
ylab = ""
)
axis(3, at = 1:9/10, labels = letters[1:9])
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)