How to avoid geom_hex hexagons getting flattened - r

I want to plot some hexagons with ggplot2 in different colors.
When I try it as the code below I get squashed hexagons
library(ggplot2)
coords <-
data.frame(x=c(1.11803398874989,0.559016994374948,-0.559016994374947,-1.11803398874989,-0.559016994374948,0.559016994374947,2.23606797749979,1.1180339887499,-1.11803398874989,-2.23606797749979,-1.1180339887499,1.11803398874989,1.73205080756888,1.22464679914735e-16),
y=c(0,0.968245836551854,0.968245836551854,1.36919674566051e-16,-0.968245836551854,-0.968245836551855,0,1.93649167310371,1.93649167310371,2.73839349132101e-16,-1.93649167310371,-1.93649167310371,1,2),
kind=c(rep("blue",12),"red","blue"))
ggplot (data = coords, aes (x = round(x,digits =13), y = round(y,digits=13), fill = kind , group = 1)) +
geom_hex (colour = "red", stat = StatIdentity) +
scale_x_continuous(expand = 0 : 1) +
scale_y_continuous(expand = c (0, 2)) +
coord_equal ()
however when I try to plot only the first 12 hexagons I don't have any problems
# fine result
ggplot (data = coords[1:12,], aes (x = round(x,digits =13), y = round(y,digits=13), fill = kind , group = 1)) +
geom_hex (colour = "red", stat = StatIdentity) +
scale_x_continuous(expand = 0 : 1) +
scale_y_continuous(expand = c (0, 2)) +
coord_equal ()
Also when I do not round the values I get an empty plot.
What could be the problem?
Thanks

I just wanted to draw 19 hexagons that do not overlap. For that purpose I was able to draw it as the following:
coords <-
data.frame(
x = c(2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 3, 5, 7),
y = c(2, 1, 2, 1, 2, 1, 2, 3, 4, 3, 4, 3, 4, 3, 4, 3, 5, 5, 5),
kind=c(rep("blue",12),"red","blue","red","blue",rep("blue",3))
)
ggplot (data = coords, aes (x = x, y = y, fill = kind , group = 1)) +
geom_hex (colour = "red", stat = StatIdentity) +
scale_x_continuous(expand = c(0:1)) +
scale_y_continuous(expand = c(0,2))
It seems that it is better to avoid floats.

Related

How to remove the default grey fill for linetype legend in barplot with ggplot2?

I have a bar-plot with two different variables.
For one of the factors (gr) I have chosen different ´lintype´ in the plot.
The legend for "gr" shows ´lintype´ but with a dark grey fill, which I think is confusing.
Does anyone know how to remove the fill or change it to white or transparent?
(All tips I have found only change a background to the legend, but does not affect the grey fill)
yval <- c(3, 7, 4, 4, 8, 9, 4, 7, 9, 6, 6, 3)
trt <- rep(c("A", "B", "C"), times=4)
gr <- rep(c(rep(("case"), times = 3), rep(("control"), times = 3)), times = 2)
var <- c(rep(("var1"), times = 6), rep(("var2"), times = 6))
df <- data.frame(yval, device, ccgroup, var)
ggplot(data=df, aes(x=var)) +
geom_bar( color = "black", size = 1, aes(weights = yval, fill = trt, linetype = gr) , position = "dodge")
This can be achieved e.g. via guide_legend which allows you to set the fill color used in the legend. Try this:
library(ggplot2)
yval <- c(3, 7, 4, 4, 8, 9, 4, 7, 9, 6, 6, 3)
trt <- rep(c("A", "B", "C"), times=4)
gr <- rep(c(rep(("case"), times = 3), rep(("control"), times = 3)), times = 2)
var <- c(rep(("var1"), times = 6), rep(("var2"), times = 6))
df <- data.frame(yval, trt, gr, var)
ggplot(data=df, aes(x=var)) +
geom_bar(color = "black", size = 1, aes(weights = yval, fill = trt, linetype = gr) , position = "dodge") +
guides(linetype = guide_legend(override.aes = list(fill = c(NA, NA))))
#> Warning: Ignoring unknown aesthetics: weights

Using geom_ridgeline with a log y-axis

I am trying to visualise timeseries data, and thought the ggridges package would be useful for this. However some of my data needs to be plotted on a log-scale. Is there a way to do this?
I tried it using y = 0.001 instead of 0, as y = zero fails, but then the heights are not correct. This can be seen when you plot the points as well.
Thanks
Example below:
data <- data.frame(x = 1:5, y = rep(0.001, 5), height = c(0.001, 0.1, 3, 300, 4))
ggplot(data) +
geom_ridgeline(aes(x, y, height = height),fill = "lightblue") +
scale_y_log10() +
geom_point(aes(x=x, y=height))
Hopefully this will give you a lead towards solving your problem.
Using an example from ggridges (https://wilkelab.org/ggridges/articles/introduction.html), I added +1 to avoid zeros (and thus Inf) when taking log10
library(ggridges)
d <- data.frame(
x = rep(1:5, 3),
y = c(rep(0, 5), rep(1, 5), rep(2, 5)),
height = c(0, 1, 3, 4, 0, 1, 2, 3, 5, 4, 0, 5, 4, 4, 1)
)
ggplot(d, aes(x, (y + 1), height = height, group = y)) +
geom_ridgeline(fill = "lightblue")+
scale_y_log10() +
annotation_logticks(sides = "l")
Generates:

Consistent size for symbols in ggsave and gganimate's 'animate'

My end goal is to create two outputs:
1) A static image showing all of my data, saved as a png
2) An animation of my data, saved as a gif.
I'm using ggplot2 and gganimate and I'm puzzled as to why the symbol size is not consistent between the two save methods.
I've tried adjusting the dpi and saving as jpg instead of png, but no luck. Can anyone help me figure out how to make the width, height, and symbol size in both output objects consistent?
Here's a reproducible example showing both outputs. You can see that the black points are smaller in the gif.
Make the png
library(gganimate)
library(ggplot2)
locs <- data.frame(x = c(1, 2, 3, 4, 5, 6),
y = c(1, 2, 3, 3.1, 3.2, 6),
LDT = c(1, 2, 3, 4, 5, 6))
g <- ggplot(locs, aes(x, y)) +
geom_point() +
theme_void() +
theme(plot.background = element_rect(fill = "pink"))
g
ggsave("test.png", g, width = 2, height = 2, dpi = 100)
Make the gif
anim <- g + transition_time(LDT)
animate(anim, duration = 1, fps = 20, width = 200, height = 200)
anim_save("test.gif")
animate() by default uses png() to generate frames.
In your ggsave call you specified a plot resolution of 100 dpi.
To get the same result using png you'll have to set res = 100 (see test_png_device.png).
Accordingly to have a consistent symbol size using animate you'll have to pass the resolution to png as an optional argument to animate as follows:
library(gganimate)
library(ggplot2)
library(gifski)
locs <- data.frame(x = c(1, 2, 3, 4, 5, 6),
y = c(1, 2, 3, 3.1, 3.2, 6),
LDT = c(1, 2, 3, 4, 5, 6))
g <- ggplot(locs, aes(x, y)) +
geom_point() +
theme_void() +
theme(plot.background = element_rect(fill = "pink"))
ggsave("test.png", g, width = 2, height = 2, dpi = 100)
png(filename = "test_png_device.png", width = 200, height = 200, units = "px", res = 100)
g
dev.off()
anim <- g + transition_time(LDT)
myAnimation <- animate(anim, duration = 1, fps = 20, width = 200, height = 200, renderer = gifski_renderer(), res = 100)
anim_save("test.gif", animation = myAnimation)
Addition: Not sure if you are interested in this, however, I like using library(plotly) for animations since it adds an animation slider by default.
Here is the ggplotly-way for your example:
library(plotly)
library(htmlwidgets)
locs <- data.frame(x = c(1, 2, 3, 4, 5, 6),
y = c(1, 2, 3, 3.1, 3.2, 6),
LDT = c(1, 2, 3, 4, 5, 6))
g <- ggplot(locs, aes(x, y)) + theme_void() +
theme(panel.background = element_rect(fill = "pink")) +
geom_point(aes(frame = LDT))
p <- ggplotly(g) %>%
animation_opts(500, easing = "linear", redraw = FALSE)
saveWidget(p, file = "myAnimation.html", selfcontained = TRUE)
browseURL("myAnimation.html")
Here a related post can be found.

Changing colours of grouped bar chart in ggplot2

For a sample dataframe:
df <- structure(list(year = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3,
3, 3, 4, 4, 4, 4, 4), imd.quintile = c(1, 2, 3, 4, 5, 1, 2, 3,
4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5), average_antibiotic = c(1.17153515458827,
1.11592565388857, 1.09288449967773, 1.07442652168281, 1.06102887394413,
1.0560582933182, 1.00678980505929, 0.992997489072538, 0.978343676071694,
0.967900478870214, 1.02854157116164, 0.98339099101476, 0.981198852494798,
0.971392872980818, 0.962289579742817, 1.00601488964457, 0.951187417739673,
0.950706064156994, 0.939174499710836, 0.934948233015044)), .Names = c("year",
"imd.quintile", "average_antibiotic"), row.names = c(NA, -20L
), vars = "year", drop = TRUE, class = c("grouped_df", "tbl_df",
"tbl", "data.frame"))
I am producing a graph detailing the differences in antibiotic prescribing BY imd.decile BY year:
ggplot(plot_data.quintiles) +
geom_col(aes(x = year, y = average_antibiotic, group=imd.quintile, fill=imd.quintile), position = "dodge") +
ylab("Antibiotic STAR-PU") +
xlab("Year") +
theme_bw() +
ylim(0, 1.5)+
scale_colour_brewer("clarity")
The blue colour choice isn't to my taste, as the differences between the imd.quintiles isn't very distinctive. I have read various posts, here, here and here, but none of which seem to answer my question.
I attempted to use the 'clarity' colours to get a wider range of colour choices. How can I correctly change the fill colour in my ggplot2 graph? what options do I have?
Is this what you want? Use factor(imd.quintile) to create discrete (categorical) data otherwise ggplot will treat numeric/integer imd.quintile as continuous.
df <- data.frame(
year = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4,
4, 4),
imd.quintile = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3,
4, 5),
average_antibiotic = c(1.17153515458827, 1.11592565388857, 1.09288449967773,
1.07442652168281, 1.06102887394413, 1.0560582933182,
1.00678980505929, 0.992997489072538, 0.978343676071694,
0.967900478870214, 1.02854157116164, 0.98339099101476,
0.981198852494798, 0.971392872980818,
0.962289579742817, 1.00601488964457, 0.951187417739673,
0.950706064156994, 0.939174499710836, 0.934948233015044)
)
library(ggplot2)
p1 <- ggplot(df) +
geom_col(aes(
x = year, y = average_antibiotic,
group = imd.quintile, fill = factor(imd.quintile)), position = "dodge") +
ylab("Antibiotic STAR-PU") +
xlab("Year") +
theme_bw() +
ylim(0, 1.5)
p1 +
scale_fill_brewer(palette = "Set2") # use scale_fill_xxx to chose the desired color palette
If you prefer continuous (sequential) colormaps, viridis or scico are good options:
p1 +
scale_fill_viridis_c(option = 'E', direction = -1)
# install.packages('scico')
library(scico)
p1 +
scale_fill_scico()
Created on 2018-11-29 by the reprex package (v0.2.1.9000)
scale_####_brewer uses palettes from RColorBrewer, there's no palette called "Clarity".
Use RColorBrewer::display.brewer.all() to see what palette's are available, then call them by name with the palette arg. Also you need to change the imd.quintile variable to be either character or factor. You're mapping your aesthetics by fill also, not colour, so you need to use scale_fill_brewer.
ggplot(df) +
geom_col(aes(x = year, y = average_antibiotic, group=imd.quintile, fill=imd.quintile), position = "dodge") +
ylab("Antibiotic STAR-PU") +
xlab("Year") +
theme_bw() +
ylim(0, 1.5) +
scale_fill_brewer(palette = "Spectral")

How to reduce the white space around a scatter plot (using R and ggplot2)

In my scatterplot, there is some empty space left and I haven't managed to reduce it manually or with the xlim() command nor with scale_x_discrete(limits=()). My code:
ggplot(data = doppelratings2_mit_ID,
aes(x = R1, y = R3)) +
geom_jitter(shape = 1, width = 0.1, height = 0.1) +
geom_smooth()+
xlab("Rater 1") +
ylab("Rater 3") +
ggtitle("Korrelation zwischen Rater 1 und 3", paste("n = 17 Texte ")) +
theme_bw(12)+
geom_abline(intercept = 0, slope = 1)+
scale_x_discrete(breaks = 1:5, labels = tick_names_5, limits = c(1:5))+
scale_y_discrete(breaks = 1:6, labels = tick_names_6, limits = c(1:6))
And the data:
> dput(doppelratings2_mit_ID_für_Stackoverflow)
structure(list(ID = c(6584209, 6598108, 6584103, 6552101, 6608303,
6656213, 9734115, 9554201, 9554108, 9604202, 6660108, 6520103,
6726215, 6574106, 9762121, 9688202, 9576108), R1 = c(2, 3, 2,
3, 3, 2, 3, 4, 4, 5, 4, 4, 2, 2, 3, 4, 4), R3 = c(2, 3, 3, 3,
2, 2, 3, 5, 5, 6, 4, 4, 2, 2, 3, 3, 4)), row.names = c(NA, -17L
), class = c("tbl_df", "tbl", "data.frame"))
Thanks for your help!
You can try
ggplot(data = d,
aes(x = R1, y = R3)) +
geom_jitter(shape = 1, width = 0.1, height = 0.1) +
geom_smooth()+
xlab("Rater 1") +
ylab("Rater 3") +
ggtitle("Korrelation zwischen Rater 1 und 3", paste("n = 17 Texte ")) +
theme_bw(12)+
geom_abline(intercept = 0, slope = 1) +
scale_x_continuous(breaks = min(d$R1):max(d$R1), labels = LETTERS[1:length(min(d$R1):max(d$R1))]) +
scale_y_continuous(breaks = min(d$R3):max(d$R3), labels = LETTERS[1:length(min(d$R3):max(d$R3))])
Then you can add + coord_cartesian(ylim=c(min(d$R3),6)) to change the limits and to recieve this plot.

Resources