How to Add Another Layer/New Series to a Ggplot

How can I add another layer / new series to a ggplot?

rubies  <- data.frame(carat = c(3, 4, 5), price= c(5000, 5000, 5000))

ggplot(diamonds, aes(carat, price)) +
geom_point() +
geom_point(data = rubies, colour = "red")

multiple layer with ggplot in R

This is the max I can produce so far. I have problems to plot density of P.S_tc as a layer because the x axis of a density plot is the variable itself: so in your case Date (desired x axis) competes with P.S_tc:

library(ggplot2)
library(hrbrthemes)

# Value used to transform the data
coeff <- 10000

# constants
CloseColor <- "#69b3a2"
P.S_EWColor <- rgb(0.2, 0.6, 0.9, 1)

p <- ggplot(example, aes(x=Date)) +

geom_line( aes(y=Close), size=1, color=CloseColor) +
geom_line( aes(y=P.S_EW * coeff), size=1, color=P.S_EWColor) +

scale_y_continuous(

# Features of the first axis
name = "Close",

# Add a second axis and specify its features
sec.axis = sec_axis(~.*coeff, name="P.S_EW ($)")
) +

theme_ipsum() +

theme(
axis.title.y = element_text(color = CloseColor, size=13),
axis.title.y.right = element_text(color = P.S_EWColor, size=13)
) +

ggtitle("Close, P.S_EW")

Sample Image

Plotting multiple time series on the same plot using ggplot()

ggplot allows you to have multiple layers, and that is what you should take advantage of here.

In the plot created below, you can see that there are two geom_line statements hitting each of your datasets and plotting them together on one plot. You can extend that logic if you wish to add any other dataset, plot, or even features of the chart such as the axis labels.

library(ggplot2)

jobsAFAM1 <- data.frame(
data_date = runif(5,1,100),
Percent.Change = runif(5,1,100)
)

jobsAFAM2 <- data.frame(
data_date = runif(5,1,100),
Percent.Change = runif(5,1,100)
)

ggplot() +
geom_line(data = jobsAFAM1, aes(x = data_date, y = Percent.Change), color = "red") +
geom_line(data = jobsAFAM2, aes(x = data_date, y = Percent.Change), color = "blue") +
xlab('data_date') +
ylab('percent.change')

Insert Layer underneath existing layers in ggplot2 object

Thanks @baptiste for pointing me in the right direction. To insert a layer underneath all other layers, just modify the layers element of the plot object.

## For example:
P$layers <- c(geom_boxplot(), P$layers)

Answer to the Bonus Question:

This handy little function inserts a layer at a designated z-level:

insertLayer <- function(P, after=0, ...) {
# P : Plot object
# after : Position where to insert new layers, relative to existing layers
# ... : additional layers, separated by commas (,) instead of plus sign (+)

if (after < 0)
after <- after + length(P$layers)

if (!length(P$layers))
P$layers <- list(...)
else
P$layers <- append(P$layers, list(...), after)

return(P)
}

adding multiple layers to a ggplot with a function

From help("+.gg"):

You can also supply a list, in which case each element of the list will be added in turn.

add_points <- function(x) {
list(geom_point(aes(x = x - 1, y = 0), color = "red"),
geom_point(aes(x = x + 1, y = 0), color = "red"))
}

p + add_points(x = 0)
#works

Add layers from different datasets

For example:

ggplot() +
geom_point(data=dat,aes(x=x,y=y,colour=z)) +
geom_line(data=dat1,aes(x=x,y=b))

if else condition in ggplot to add an extra layer

What you are seeing is a syntax error. The most robust way I can think of is:

tmp.data<-c(1,2,3) 
if(tmp.data[1]!="no value") {
p = p + geom_point()
}
p + geom_line()

So you compose the object p in a sequence, only adding geom_point() when the if statements yields TRUE.

Ggplot and sf for overlaying two layers of polygons (.shp)

Every geom_SOMETHING() function has a data argument where you can configure the data you are using. This argument plays the same role as the data argument in the ggplot() function. When you specify data in ggplot, all the other geom_SOMETHING() function inherit the argument. The same happens with the arguments in aes()

So the first recommendation is remove the data = map1 and aes arguments from ggplot and add it to the geom_sf function.

g <- ggplot() + geom_sf(map1, aes(fill = var1)) + geom_sf(map2)


Related Topics



Leave a reply



Submit