making a single gridline thicker in ggplot2 - r

I have a plot of a bunch of values between -1 and 1. Let's say it looks like this:
data <- data.frame(x=1:20,y=runif(20)*2-1)
p <- ggplot(data=data,aes(x=x,y=y)) + geom_line() + scale_y_continuous(breaks=seq(-1,1,0.5),limits=c(-1,1))
p
What I want is for the horizontal line with y=0, to be thicker than all the other gridlines. Is there a simple way to do this?

You can add a line at y = 0 via geom_hline. Put this layer before geom_line so it is behind the rest of the plot.
ggplot(data=data,aes(x=x,y=y)) +
geom_hline(yintercept = 0, color = "white", size = 2) +
geom_line() +
scale_y_continuous(breaks=seq(-1,1,0.5),limits=c(-1,1))

Related

How to print Frequencies on top of Histogram bars in ggplot

I would like to know if it is possible to display the frequencies at the top of each counting bar in a ggplot histogram.
This is the code I have got so far:
br <- seq(0, 178, 10)
ggplot(dfAllCounts, aes(x=months)) + geom_histogram(aes(months), bins = 30, fill="#d2aa47", color = '#163B8B', size = .8, alpha = 0.3) +
scale_x_continuous(breaks = br)
I would like to display that number of months on top, thanks
Instead of the geom_histogram wrapper, switch to the underlying stat_bin function, where you can use the built in geom="text", combined with the after_stat(count) to add the label to a histogram.
ggplot(mpg,aes(x=displ)) +
stat_bin(binwidth=1) +
stat_bin(binwidth=1, geom="text", aes(label=after_stat(count)), vjust=0)
Modified from https://stackoverflow.com/a/24199013/10276092

format internal lines of a stacked geom_bar ggplot

I want to remove the internal borders from my ggplot, leaving a coloured border around the outside of each bar only. Here is a test data frame, with a stacked bar plot. Ideally, I will end up with the groups in the stack still being a shade of grey, with a colourful outline per box.
test <- data.frame(iso=rep(letters[1:5],3),
num= sample(1:99, 15, replace=T),
fish=rep(c("pelagic", "reef", "benthic"), each=5),
colour=rep(rainbow(n=5),3))
ggplot(data=test, aes(x=iso, y=num, fill=fish, colour=colour)) +
geom_bar(stat="identity") +
theme_bw() +
scale_colour_identity() + scale_fill_grey(start = 0, end = .9)
You can accomplish this by moving the fill and colour aes() settings into two separate geom_bar() elements: one which takes the sum for each iso value (the outline), and another which splits things up by fish:
ggplot(data=test, aes(x=iso, y=num)) +
geom_bar(stat="summary", fun.y="sum", aes(color=colour)) +
geom_bar(stat="identity", aes(fill=fish)) +
theme_bw() +
scale_colour_identity() +
scale_fill_grey(start = 0, end = .9)

ggplot: add multiline text annotation outside of plot

I want to add a textbox of 10 separate, stacked lines outside of my plot area in ggplot. My text is: t = c("a=1", "b=2", "c=3", ... , "j=10") but these labels are independent of the data.frame that I made my original ggplot. How can I add 10 lines outside of the plot area?
For example, I want to add a textbox around my vector t on the right of the following plot:
df = data.frame(y=rnorm(300), test=rep(c(1,2,3),each=100))
t = c("a=1", "b=2", "c=3", "d=4", "e=5", "f=6", "g=7", "h=8", "i=0", "j=10")
p <- ggplot(df, aes(x=factor(test), y=y))
p <- p + geom_violin() + geom_jitter(height=0, width=0.1)
p <- p + theme(legend.title=element_blank(), plot.margin=unit(c(0.1, 3, 0.1, 0.1), "cm"))
p
try
library(gridExtra)
grid.arrange(p, right = tableGrob(matrix(t,ncol=1),
theme = ttheme_minimal(padding = unit(c(3,1),"line"))))
You can create a geom_text layer using the label values in t in order to get the labels printed as a legend. But we set alpha=0 in geom_text so that these labels won't be included in the plot, and we use legend.key=element_blank() and override.aes(list(size=0)) to get the "legend" labels (the t values) printed without the meaningless legend key.
p +
geom_text(data = data.frame(t, test=NA, y=NA), aes(label=t, colour=t), alpha=0, x=1, y=1) +
theme(legend.key=element_blank(),
legend.margin=margin(l=-10)) +
guides(colour=guide_legend(override.aes=list(size=0)))

ggplot2 legend, geom_abline and stat_gg

I have been trying to plot a Normal Q-Q plot with a red line across, in R with ggplot2. I have been unable to add a legend (with LaTeX math) to explain the red line
Here is the code for the basic figure:
ggplot(stdres_df, aes(sample=stdres)) +
stat_qq(color="black") +
geom_abline(slope = 1,
intercept = 0, color ="red")
Thanks in advance.
To get a legend, you need to map something to a color aesthetic inside a call to aes(). In this case, there's no grouping variable to map to colour, but you can just map colour to the name you want to use for the red line.
The line will be red by default, because ggplot uses hcl(15, 100, 65) (a light red) as the first color in its default color palette. However, you can set the color to whatever you want using scale_colour_manual, as shown in the example below. For example:
set.seed(2)
df <- data.frame(y = rnorm(200))
ggplot(df, aes(sample = y)) +
stat_qq() +
geom_abline(aes(slope=1, intercept=0, colour="Test"), size=1) +
coord_equal(xlim=range(df$y)) +
labs(colour="") +
scale_colour_manual(values="red")
Something like this?
ggplot() +
stat_qq(aes(sample=1:100), distribution = qt,dparams = list(df=5)) +
geom_abline(aes(linetype = "line"), slope = 1, intercept = 0, color ="red") +
geom_text(aes(3, 0, label = "TEXT HERE"))

R - ggplot2 - Add arrow if geom_errorbar outside limits

I am creating a figure using ggplot and would like to use arrows to indicate where my error bars go beyond the defined axis. For example, I would like to end up with a figure that looks like:
I want R to determine which lower bounds are outside the defined chart range and to add a nice looking arrow (instead of my ugly paint added arrows).
I know there has to be a way to do this. Any ideas? Here is my code to make the above graph without the arrows added by-hand:
#generate data
myData<-data.frame(ALPHA=round(runif(60,.5,.8),2),
error=round(runif(60,.05,.15),2),
formN=rep(1:5,12),
Cat=c(rep("ELL",30),rep("SWD",30)),
grade=rep(c(rep(3,5),rep(4,5),rep(5,5),rep(6,5),rep(7,5),rep(8,5)),2)
)
myData$LCL<-myData$ALPHA-myData$error
myData$UCL<-myData$ALPHA+myData$error
#set error outside of range for example
myData[myData$Cat=="ELL" & formN==1,"LCL"]<-0
library(ggplot2)
ggplot(myData, aes(x=formN, y=ALPHA, colour=Cat)) +
geom_errorbar(aes(ymin=LCL, ymax=UCL), width=.4, position=position_dodge(.5)) +
geom_point(position=position_dodge(.5), size=2) +
labs(x="Form", y="Alpha", title="TITLE") +
geom_line(position=position_dodge(.5), size=.3) +
coord_cartesian(ylim=c(.3, 1)) +
facet_wrap(~grade, ncol=3)
What about this: first create a column to check if the values go beyond your range and if this is the case determine the length from the y-point to the border of the plot.
library(dplyr)
myData_m <- myData %>% mutate(LCL_l = ifelse(LCL < .3, ALPHA - .3, NA), UCL_l = ifelse(UCL > 1, 1 - ALPHA, NA))
In the second step use this variable to add arrows with segment. If there are also values going through the upper limit you can additionally use the other variable ULC_l to add further arrows.
ggplot(myData_m, aes(x=formN, y=ALPHA, colour=Cat)) +
geom_errorbar(aes(ymin=LCL, ymax=UCL), width=.4, position=position_dodge(.5)) +
geom_point(position=position_dodge(.5), size=2) +
labs(x="Form", y="Alpha", title="TITLE") +
geom_line(position=position_dodge(.5), size=.3) +
coord_cartesian(ylim=c(.3, 1)) +
facet_wrap(~grade, ncol=3) +
geom_segment(aes(x = formN - .12, xend = formN - .12, y = ALPHA, yend = ALPHA - LCL_l), arrow = arrow(length = unit(myData_m$LCL_l, "cm")))
P.S.: the -.12 is used to get rid of the dodging effect to the arrows.

Resources