How can I bring my yticklabels closer to geom_tile? - r

I've been trying to recreate some heatmaps from this, but my y ticks are quite far apart from the remainder of geom_tile.
Here is my code
library(tidyverse)
library(ggthemes)
library(viridis)
d <-c('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')
days <- factor(d, levels = d,ordered=TRUE)
L = rnorm(7*24,0,20)
df <- data.frame(days,L) %>%arrange(days)
df['Time'] = c(0:23)
gg <- ggplot(data = df, aes(x = Time, y = days, fill = L))+
geom_tile(color = 'white',size = 0.1)+
scale_fill_viridis('')+
coord_equal()+
labs(x = NULL, y = NULL, title = 'Practice HeatMap')+
theme_tufte(base_family="Helvetica")+
theme(axis.ticks=element_blank())
print(gg)
and the resulting plot
Even using hjust wont move the labels closer. How can I achieve a cleaner look?

Related

geom_segment reaching over multiple facets

I am trying to draw a line with R's ggplot that starts on one facet and ends on another.
I believe this question was not asked yet (at least I could not find it) but I have found some example code that achieves exactly this: http://rstudio-pubs-static.s3.amazonaws.com/410976_f8eb6b218bfa42038a8b7bc9a6f9a193.html
However, documentation is weak and I did not manage to untangle the code.
Can someone please provide an easily understandable version that illustrates the trick?
Here's some code as an example:
library(ggplot2)
df <- data.frame(x = 1:6, y = 1:6, facet = c(rep('A', times = 3), rep('B', times = 3)))
gg <- ggplot(data = df, mapping = aes(x = x, y = y)) + facet_grid(~ facet) +
geom_line()
gg
line <- data.frame(x = 3, y = 3,
xend = 4, yend = 4,
facet = 'A')
gg_line <- gg + geom_segment(data = line, mapping = aes(x = x, y = y,
xend = xend, yend = yend),
inherit.aes = FALSE, color = 'red')
gg_line
Obviously, in gg_line, the red geom_segment reaches the respective coordinates in facet A.
However, I would like the end points to refer to the coordinates in facet B.
Any nudges to a working solution are highly appreciated!

ggplot : Plot two bars and one line?

I need to plot two bars and one line. I have 3 data frames as this:
require(ggplot2)
df.0 <- data.frame(x = c(1:5), y = rnorm(5))
df.1 <- data.frame(x = c(1:5), y = rnorm(5))
df.2 <- data.frame(x = c(1:5), y = runif(5))
ggplot(df.0, aes(x=x, y=y)) +
geom_line(aes(x=x, y=y))+
geom_bar(data=df.1, aes(x=x, y=y),stat = "identity",position="dodge")+
geom_bar(data=df.2, aes(x=x, y=y),stat = "identity",position="dodge")
I can't manage to plot the bars and the line in the correct way. It should look as the image below.
I'm not familiar with ggplot2. I've read a lot of links, and I can't find a post similar to my question.
Thanks for your time and interest.
Combine the data frames - at least the two for the bar plot. Dodging is done within a single geom_bar layer, not between two separate ones.
df_bar = rbind(df.1, df.2)
df_bar$id = rep(c("df.1", "df.2"), times = c(nrow(df.1), nrow(df.2)))
ggplot(df.0, aes(x = x, y = y)) +
geom_line() +
geom_col(data = df_bar, aes(fill = id), position="dodge")
Other changes: no need to repeat aes(x = x, y = y) in every layer. If it's in the original ggplot() it will be inherited. Also geom_col is a nice way of geom_bar(stat = 'identity').

Plotly hovering on tiles in the background of a cloud of points - how to show the tooltip?

I'm converting a ggplot2 plot to plotly.
The plot consists of a tile layer (in the background) and a point layer (in the foreground).
I would like to have tooltips when hovering on the tiles.
The code below mostly gets me what I am looking for. When I hover on tiles in "points free" zones, the desired tooltip appears. However, when I hover in areas with a high density of dots, the tooltips do not appear.
I thought that playing with the layerData parameter in the ggplotly call might help, but that was not the case.
library(ggplot2)
library(dplyr)
library(plotly)
set.seed(1)
dat_points <- data.frame(x = rnorm(100), y = rnorm(100))
dat_tiles <- expand.grid(tx = -3:3, ty = -3:3)
dat_tiles$val <- rnorm(nrow(dat_tiles))
dat_tiles$label <- sample(LETTERS[1:5], nrow(dat_tiles), replace = T)
p <- ggplot() +
geom_tile(data = dat_tiles, aes(x = tx, y = ty, fill = val, text = label)) +
geom_point(data = dat_points, aes(x = x, y = y), alpha = .5)
gg <- ggplotly(p, tooltip = "text")
gg
I would like that hovering on high density areas (e.g. 0, 0) would bring up tooltips with the same promptness as in low density areas.
EDIT: added static image of the plot.
You can switch the order of you layers in p and because of how ggplotly() constructs from a ggplot object, you get an identical looking plot, but with the desired tooltip behavior!
p <- ggplot() +
geom_point(data = dat_points, aes(x = x, y = y), alpha = 1) +
geom_tile(data = dat_tiles, aes(x = tx, y = ty, fill = val, text = label))
p # this looks bad
gg <- ggplotly(p, tooltip = "text")
gg # but this looks good!

Adjusting axis in ggtern ternary plots

I am trying to generate a ternary plot using ggtern.
My data ranges from 0 - 1000 for x, y,and z variables. I wondered if it is possible to extend the axis length above 100 to represent my data.
#Nevrome is on the right path, your points will still be plotted as 'compositions', ie, concentrations sum to unity, but you can change the labels of the axes, to indicate a range from 0 to 1000.
library(ggtern)
set.seed(1)
df = data.frame(x = runif(10)*1000,
y = runif(10)*1000,
z = runif(10)*1000)
breaks = seq(0,1,by=0.2)
ggtern(data = df, aes(x, y, z)) +
geom_point() +
limit_tern(breaks=breaks,labels=1000*breaks)
I think there is no direct solution to do this with ggtern. But an easy workaround could look like this:
library(ggtern)
df = data.frame(x = runif(50)*1000,
y = runif(50)*1000,
z = runif(50)*1000,
Group = as.factor(round(runif(50,1,2))))
ggtern() +
geom_point(data = df, aes(x/10, y/10, z/10, color = Group)) +
labs(x="X", y="Y", z="Z", title="Title") +
scale_T_continuous(breaks = seq(0,1,0.2), labels = 1000*seq(0,1,0.2)) +
scale_L_continuous(breaks = seq(0,1,0.2), labels = 1000*seq(0,1,0.2)) +
scale_R_continuous(breaks = seq(0,1,0.2), labels = 1000*seq(0,1,0.2))

Barplot with horizontal marker and asterisks in R (ggplot etc)

Using R's standard plot or better still GGPLOT,
is there a way to create a plot like this?
Note especially the horizontal lines across selected bar
with asterisk on top of it.
I don't know of an easy way to annotate graphs like this in ggplot2. Here's a relatively generic approach to make the data you'd need to plot. You can use a similar approach to annotate the relationships as necessary. I'll use the iris dataset as an example:
library(ggplot2)
library(plyr) #for summarizing data
#summarize average sepal length by species
dat <- ddply(iris, "Species", summarize, length = mean(Sepal.Length))
#Create the data you'll need to plot for the horizontal lines
horzlines <- data.frame(x = 1,
xend = seq_along(dat$Species)[-1],
y = seq(from = max(dat$length), by = 0.5, length.out = length(unique(dat$Species))-1),
yend = seq(from = max(dat$length), by = 0.5, length.out = length(unique(dat$Species))-1),
label = c("foo", "bar")
)
ggplot() +
geom_histogram(data = dat, aes(Species, length), stat = "identity") +
geom_segment(data = horzlines, aes(x = x, xend = xend, y = y, yend = yend)) +
geom_text(data = horzlines, aes(x = (x + xend)/2, y = y + .25, label = label))
Giving you something like this:

Resources