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()
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)
*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
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
You can change the axis labels pretty easily if you need to.
Related Topics
How to Check If a Vector Contains N Consecutive Numbers
Fama MACbeth Standard Errors in R
Rolling Regression by Group in the Tidyverse
Mapping Specific States and Provinces in R
How to Test If Object Is a Vector
How to Make an Overlapping Barplot
Extract Time Series of a Point ( Lon, Lat) from Netcdf in R
How to Print the Structure of an R Object to the Console
How to Call the 'Function' Function
How to Remove All Rows from a Data.Frame
Stacke Different Plots in a Facet Manner
Sine Curve Fit Using Lm and Nls in R
Align Plots Next to Each Other with Knitr
How to Prevent Objects from Automatically Loading When I Open Rstudio
Grouping with Custom Geom Fails - How to Inspect Internal Object from Draw_Panel()
Combined Line & Bar Geoms: How to Generate Proper Legend
Check If Character String Is a Valid Color Representation
What Are Productive Ways to Debug Rcpp Compiled Code Loaded in R (On Os X Mavericks)