Can Ggplot Make 2D Summaries of Data

2D summary plot with counts as labels

While writing the question, which took some hours of testing, I found a solution: adding a fill=NULL, or fill=mean(value) in the text one gives me what I want. Below the code and their resulting plots; the only difference is the label of the legend.

But it feels very hacky, so I would appreciate a better solution.

ggplot(dat) +
aes(x = lon, y = lat, z = value) +
stat_summary_hex(bins = 5, fun = "mean", geom = "hex") +
stat_binhex(aes(label = ..count.., fill = NULL), bins = 5, geom = "text") +
theme_bw()

ggplot(dat) +
aes(x = lon, y = lat, z = value) +
stat_summary_hex(bins = 5, fun = "mean", geom = "hex") +
stat_binhex(aes(label = ..count.., fill = mean(value)), bins = 5, geom = "text") +
theme_bw()

Sample Image

add text for each cell of a 2D matrix plot in R (plotrix and/or ggplot2)

Here is a way using ggplot2.

library(reshape2)
library(ggplot2)

testdf = data.frame(x = c(0,1,1),y = c(1,1,2),z = c(1,2,2))

# Convert testdf dataframe to a matrix.
df <- as.matrix(testdf)
# Flip the matrix horizontally, and put into long format.
df<-df[c(3:1), ,drop = FALSE] %>%
melt()

# Create a new dataframe with the names for each tile.
testdf_names <- data.frame(x = c("A","C","B"),y = c("AB","CD","C"),z = c("B","E","D"))
#Then, do the same process as above by first putting into a matrix.
df_names <- as.matrix(testdf_names)
# Flip the matrix horizontally, and put into long format.
df_names<-df_names[c(3:1), ,drop = FALSE] %>%
melt() %>%
dplyr::rename(names = value)

# Join the dataframes together for plotting.
df_new <- df %>%
dplyr::group_by(Var1, Var2) %>%
dplyr::left_join(df_names)

# Plot.
ggplot(df_new, aes(x = Var2, y = Var1)) +
geom_tile(aes(fill=as.factor(value)), color = "black", size = 1) +
scale_fill_manual(values=c("red", "yellow", "green")) +
theme_void() +
theme(legend.position="none") +
geom_text(aes(label=names), size = 30)

Sample Image

*Note: It isn't necessary to convert to a matrix; it is just a preference.

Add a line to a 2D density plot using ggplot2 in R

Two problems, first as @aosmith says you need to set density specific to the aesthetic in geom_tile. secondly there's a typo in geom_line(aes(x=x,y=x)

So

erupt <- ggplot(df, aes(waiting, eruptions)) +
geom_tile(aes(fill=density)) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0))

mydf <- data.frame(x=c(50,60,70),y=c(2,3,4))
erupt + scale_fill_gradient(limits = c(0, 0.04), low = "white", high = "black") +
geom_line(aes(x=x,y=y), data = mydf)

Produces

Sample Image

2D Normal PDF with ggplot and R

# reshape the data
require(reshape2)
dat <- melt(z)

# use geom_raster to mimic image

gg <- ggplot(dat, aes(x=Var2, y=Var1, fill=value))
gg <- gg + labs(x="", y="")
gg <- gg + geom_raster()
gg <- gg + coord_equal()
gg <- gg + scale_fill_gradient(low="red", high="yellow")
gg <- gg + scale_x_continuous(expand = c(0, 0))
gg <- gg + scale_y_continuous(expand = c(0, 0))
gg <- gg + theme_bw()
gg

Sample Image

You can change the axis labels pretty easily if you need to.



Related Topics



Leave a reply



Submit