Add a function to ggplot - r

I would like to add this function
ma <- function(x,n=11){filter(x,rep(1/n,n),method = "convolution", sides=2)}
`
to my data:
ggplot(data, aes(y=volume, x=time)) +
geom_line(color= "black", size=0.2, alpha=0.9) +
theme_classic() +
ggtitle(" Volume")

Since you did not provide any sample data, here's a solution based on some random dummy data:
library(ggplot2)
ma <- function(x,n=11){filter(x,rep(1/n,n),method = "convolution", sides=2)}
x <- runif(1000)
data = data.frame(time = x, volume <- ma(x))
ggplot(data, aes(y=volume, x=time)) +
geom_line(color= "black", size=0.2, alpha=0.9) +
theme_classic() +
ggtitle(" Volume")

Related

Adding custom legend after theme(legend.title=element_blank(), legend.position='none')

I have the following data:
set.seed(100)
vals <- rnorm(100)
groups <- c(rep('group a', 30), rep('group b', 70))
df <- data.frame('vals'=vals, 'groups'=groups)
I plot the distributions of vals within groups like this:
ggplot(df, aes(y=vals, x=groups, fill=groups)) +
geom_boxplot() +
theme_minimal() +
scale_fill_brewer(palette = "Set3") +
geom_hline(yintercept=0.5, color='red', lty='dashed', size=1) +
geom_hline(yintercept=-0.5, color='blue', lty='dashed', size=1) +
theme(legend.title=element_blank(), legend.position='none')
This produces the following picture.
I would like to include a legend for the blue and red horizontal lines but not for the boxplots. How do I do that?
You could use the aes of linetype for each geom_hline with scale_linetype_manual and say that the boxplot should not be shown like this:
set.seed(100)
vals <- rnorm(100)
groups <- c(rep('group a', 30), rep('group b', 70))
df <- data.frame('vals'=vals, 'groups'=groups)
library(ggplot2)
ggplot(df, aes(y=vals, x=groups, fill=groups)) +
geom_boxplot(show.legend = FALSE) +
theme_minimal() +
scale_fill_brewer(palette = "Set3") +
geom_hline(aes(yintercept=0.5, lty='red'), color='red', size=1) +
geom_hline(aes(yintercept=-0.5, lty='blue'), color='blue', size=1) +
scale_linetype_manual(name = "Legend", values = c(2, 2),
guide = guide_legend(override.aes = list(color = c("red", "blue"))))
Created on 2023-01-16 with reprex v2.0.2

How to fit a lot of x labels or space out x labels in ggplot2

Is there a way to space out so that each x label is more distinguishable?
I feel like this question has been asked before but I can't seem to find an answer. I believe the graph needs to be bigger for this to work, is their a way to make the graph bigger within Rstudio? or make the text smaller
My code so far :
bar_plt = ggplot(data, aes(fct_infreq(Event))) + geom_bar(fill = "dodgerblue", width = .4) +
xlab("Event Names") + ylab("Number of Observations") + coord_flip()
TIA
what about working with labels in this way (sorry for the fake data, but I have not got a sample of your):
library(ggplot2)
# numbers
set.seed(1)
y<-sample(1:30, 500, TRUE)
# very long and numerous labels
x <- paste(sample(letters[1:22], 500, TRUE),sample(letters[1:2], 500, TRUE),'abcdefghijklmnopqrstuvwxyz')
data <- data.frame(x,y)
# simple ggplot barplot
p <- ggplot(data, aes(x = x, y = y)) + geom_bar(stat = "identity") + coord_flip()
# play with the size to have a fitting dimension
p <- p + theme(axis.text.y = element_text(face="bold", color="black", size=8))
# you can also abbreviate the labels if necessary
p <- p + scale_x_discrete(labels = abbreviate)
p
Your plot could be something like:
library(forcats)
library(ggplot2)
# data
set.seed(1)
Events <- paste(sample(letters[1:22], 500, TRUE),sample(letters[1:2], 500, TRUE),'abcdefghijklmnopqrstuvwxyz')
data <- data.frame(Events)
bar_plt <- ggplot(data, aes(fct_infreq(Events))) + geom_bar(fill = "dodgerblue", width = .4) + coord_flip()
bar_plt <- bar_plt + xlab("Event Names") + ylab("Number of Observations")
bar_plt <- bar_plt + theme(axis.text.y = element_text(face="bold", color="black", size=8))
bar_plt <- bar_plt + scale_x_discrete(labels = abbreviate)
bar_plt

Displaying the number of counts per bin in a ggplot2 hexbin graph

I'm working on a project to simulate the movement of missing ships. I've made a distribution map using this code:
library("ggplot2")
a <- rnorm(1000, 30.2, 2)
b <- rnorm(1000, 10, 5)
y <- (x + a + b) * 0.6
df <- data.frame(x,y)
p <- ggplot(df,aes(x=x,y=y)) +
ggtitle("A Priori Map") + xlab("Longtitude") + ylab("Latitude") +
scale_fill_gradientn(colors = topo.colors(10))
p + stat_binhex(show.legend = T, bins = 20)
This produces a map like this:
A hexbin map
However, instead of showing the number of counts using a color, I would like to show the actual count in a point. So if the program 'landed' on a certain point 3 times, it would display '3'.
How can this be done in R?
Here's how to add counts to the existing graph:
library(ggplot2)
theme_set(theme_bw())
set.seed(2)
a <- rnorm(1000, 30.2, 2)
b <- rnorm(1000, 10, 5)
x = rnorm(1000)
y <- (x + a + b) * 0.6
df <- data.frame(x,y)
p <- ggplot(df,aes(x=x,y=y)) +
ggtitle("A Priori Map") +
xlab("Longtitude") + ylab("Latitude") +
scale_fill_gradientn(colors = topo.colors(10)) +
stat_binhex(show.legend = T, bins = 20)
p + geom_text(stat="binhex", bins=20, aes(label=..count..), show.legend=FALSE,
colour=hcl(15,100,60), fontface="bold", size=3.5)
To remove the fill colours, you could do:
ggplot(df,aes(x=x,y=y)) +
ggtitle("A Priori Map") +
xlab("Longtitude") + ylab("Latitude") +
stat_binhex(bins = 20, fill=NA, colour="black") +
geom_text(stat="binhex", bins=20, aes(label=..count..), colour="red")
You could also use text size to highlight the regions of highest density:
ggplot(df,aes(x=x,y=y)) +
ggtitle("A Priori Map") +
xlab("Longtitude") + ylab("Latitude") +
stat_binhex(show.legend = T, bins = 20, fill=NA, colour="grey70") +
geom_text(stat="binhex", bins=20, aes(label=..count.., size=..count..), colour="red") +
scale_size_continuous(range=c(3,6)) +
guides(size=FALSE)
Which also works without the hex-grid:
ggplot(df,aes(x=x,y=y)) +
ggtitle("A Priori Map") +
xlab("Longtitude") + ylab("Latitude") +
geom_text(stat="binhex", bins=20, aes(label=..count.., size=..count..), colour="red") +
scale_size_continuous(range=c(3,6)) +
guides(size=FALSE)

Deleting an entire row of facets of unused factor level combination

I want to remove the 2nd row of facets from my plot below because there is no data for that factor combination.
library(ggplot2)
library(grid)
set.seed(5000)
# generate first df
df1 = data.frame(x=rep(rep(seq(2,8,2),4),6),
y=rep(rep(seq(2,8,2),each=4),6),
v1=c(rep("x1",32),rep("x2",64)),
v2=c(rep("y1",64),rep("y2",32)),
v3=rep(rep(c("t1","t2"),each=16),3),
v4=rbinom(96,1,0.5))
# generate second df
df2 = data.frame(x=runif(20)*10, y=runif(20)*10,
v1=sample(c("x1","x2"),20,T))
# plot
ggplot() +
geom_point(data=df1, aes(x=x, y=y, colour = factor(v4)), shape=15, size=5) +
scale_colour_manual(values = c(NA,"black")) + facet_grid(v1+v2~v3, drop = T) +
geom_point(data=df2, aes(x=x,y=y), shape=23 , colour="black", fill="white", size=4) +
coord_equal(ratio=1) + xlim(0, 10) + ylim(0, 10)
I tried to use the idea from this post..
g=ggplotGrob(y)
pos=which(g$layout$t==5 | g$layout$t==6)
g$layout=g$layout[-c(pos),]
g$grobs=g$grobs[-c(pos)]
grid.newpage()
grid.draw(g)
..but got this.
How do I eliminate the white space? Also, is there a straightforward solution to this, without having to manipulate the grobs, etc?
Just modify the data:
df2 <- rbind(cbind(df2, v2 = "y1"),
cbind(df2, v2 = "y2"))
df2 <- df2[!(df2$v1 == "x1" & df2$v2 == "y2"),]
# plot
ggplot() +
geom_point(data=df1, aes(x=x, y=y, colour = factor(v4)), shape=15, size=5) +
scale_colour_manual(values = c(NA,"black")) + facet_grid(v1+v2~v3, drop = T) +
geom_point(data=df2, aes(x=x,y=y), shape=23 , colour="black", fill="white", size=4) +
coord_equal(ratio=1) + xlim(0, 10) + ylim(0, 10)

Draw lines between two facets in ggplot2

How can I draw several lines between two facets?
I attempted this by plotting points at the min value of the top graph but they are not between the two facets. See picture below.
This is my code so far:
t <- seq(1:1000)
y1 <- rexp(1000)
y2 <- cumsum(y1)
z <- rep(NA, length(t))
z[100:200] <- 1
df <- data.frame(t=t, values=c(y2,y1), type=rep(c("Bytes","Changes"), each=1000))
points <- data.frame(x=c(10:200,300:350), y=min(y2), type=rep("Bytes",242))
vline.data <- data.frame(type = c("Bytes","Bytes","Changes","Changes"), vl=c(1,5,20,5))
g <- ggplot(data=df, aes(x=t, y=values)) +
geom_line(colour=I("black")) +
facet_grid(type ~ ., scales="free") +
scale_y_continuous(trans="log10") +
ylab("Log values") +
theme(axis.text.x = element_text(angle = 90, hjust = 1), panel.margin = unit(0, "lines"))+
geom_point(data=points, aes(x = x, y = y), colour="green")
g
In order to achieve that, you have to set the margins inside the plot to zero. You can do that with expand=c(0,0). The changes I made to your code:
When you use scale_y_continuous, you can define the axis label inside that part and you don't need a seperarate ylab.
Changed colour=I("black") to colour="black" inside geom_line.
Added expand=c(0,0) to scale_x_continuous and scale_y_continuous.
The complete code:
ggplot(data=df, aes(x=t, y=values)) +
geom_line(colour="black") +
geom_point(data=points, aes(x = x, y = y), colour="green") +
facet_grid(type ~ ., scales="free") +
scale_x_continuous("t", expand=c(0,0)) +
scale_y_continuous("Log values", trans="log10", expand=c(0,0)) +
theme(axis.text.x=element_text(angle=90, vjust=0.5), panel.margin=unit(0, "lines"))
which gives:
Adding lines can also be done with geom_segment. Normally the lines (segments) will appear in both facets. If you want them to appear between the two facets, you will have to restrict that in data parameter:
ggplot(data=df, aes(x=t, y=values)) +
geom_line(colour="black") +
geom_segment(data=df[df$type=="Bytes",], aes(x=10, y=0, xend=200, yend=0), colour="green", size=2) +
geom_segment(data=df[df$type=="Bytes",], aes(x=300, y=0, xend=350, yend=0), colour="green", size=1) +
facet_grid(type ~ ., scales="free") +
scale_x_continuous("t", expand=c(0,0)) +
scale_y_continuous("Log values", trans="log10", expand=c(0,0)) +
theme(axis.text.x=element_text(angle=90, vjust=0.5), panel.margin=unit(0, "lines"))
which gives:

Resources