ggplot2

Adding percentage labels to ggplot when using stat_count

有些话、适合烂在心里 提交于 2021-02-08 05:16:26
问题 For some reason, I can't seem to be able to add correct proportion labels to a ggplot by using stat_count . The code below returns labels that display 100% for all categories, even though I'm using ..prop.. . Should I use something else instead of stat_count ? library(tidyverse) diamonds %>% ggplot(aes(color, fill=cut)) + geom_bar(position = 'fill') + stat_count(aes(label= scales::percent(..prop..)), geom = 'text', position = position_fill(vjust = 0.5)) I know this can also be accomplished by

R convert grid units of layout object to native

百般思念 提交于 2021-02-08 05:15:40
问题 My problem is somewhat related to Convert units from npc to native using grid in R . I'm trying to figure out the location of certain plot elements start in a ggplot2 object (axes, main plot, etc). I found the following code: rm(list = ls()) library(ggplot2) library(grid) library(gtable) # a dummy plot g <- ggplot(cars, aes(x = speed, y = dist)) + geom_point() g # a layout of each element obj <- ggplotGrob(g) l <- gtable:::gtable_layout(obj) grid:::grid.show.layout(l) All the information I

R convert grid units of layout object to native

北城余情 提交于 2021-02-08 05:14:35
问题 My problem is somewhat related to Convert units from npc to native using grid in R . I'm trying to figure out the location of certain plot elements start in a ggplot2 object (axes, main plot, etc). I found the following code: rm(list = ls()) library(ggplot2) library(grid) library(gtable) # a dummy plot g <- ggplot(cars, aes(x = speed, y = dist)) + geom_point() g # a layout of each element obj <- ggplotGrob(g) l <- gtable:::gtable_layout(obj) grid:::grid.show.layout(l) All the information I

Manual breaks of facet_wrap() in ggplot2

微笑、不失礼 提交于 2021-02-08 05:09:10
问题 Problem I am trying to understand how to provide y-axis breaks manually to facets, generated by the facet_wrap() function in ggplot2 in R . Minimal reproducible example Disclaimer: I have borrowed the following example from the R studio community With the following code, you can specify the y-axis breaks based on the y-axis values in de dataset. library(ggplot2) diamonds2 <- subset(diamonds, !(cut == "Fair" & price > 5000)) my_breaks <- function(x) { if (max(x) < 6000) seq(0, 5000, 1000) else

Manual breaks of facet_wrap() in ggplot2

Deadly 提交于 2021-02-08 05:07:48
问题 Problem I am trying to understand how to provide y-axis breaks manually to facets, generated by the facet_wrap() function in ggplot2 in R . Minimal reproducible example Disclaimer: I have borrowed the following example from the R studio community With the following code, you can specify the y-axis breaks based on the y-axis values in de dataset. library(ggplot2) diamonds2 <- subset(diamonds, !(cut == "Fair" & price > 5000)) my_breaks <- function(x) { if (max(x) < 6000) seq(0, 5000, 1000) else

Make stat_ellipse {ggplot2} outline geom_point fill color

早过忘川 提交于 2021-02-08 05:07:35
问题 I have a scatterplot with 15 groupings. I am using geom_point() with shape = 21 so that I can have fill and color (outline color). I am using black for the outline color to give better contrast between the similar colors in my legend. When I add a stat_ellipse() though, it makes the ellipse outline black. I want this, with black outlines around the points: groupings <- paste0("Group", 1:15) iris$group <- rep(groupings, 10) iris_plot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +

Manual breaks of facet_wrap() in ggplot2

落爺英雄遲暮 提交于 2021-02-08 05:05:59
问题 Problem I am trying to understand how to provide y-axis breaks manually to facets, generated by the facet_wrap() function in ggplot2 in R . Minimal reproducible example Disclaimer: I have borrowed the following example from the R studio community With the following code, you can specify the y-axis breaks based on the y-axis values in de dataset. library(ggplot2) diamonds2 <- subset(diamonds, !(cut == "Fair" & price > 5000)) my_breaks <- function(x) { if (max(x) < 6000) seq(0, 5000, 1000) else

How to align table and plot in rmarkdown html_document

青春壹個敷衍的年華 提交于 2021-02-08 05:01:54
问题 How can I align a kable table to be adjacent to a ggplot2 plot in an rmarkdown html_document? Foo.Rmd --- title: "Foo" output: html_document --- ```{r setup, include=FALSE} library(ggplot2) library(knitr) library(kableExtra) ``` # Table next to plot ```{r echo = FALSE} kable(head(iris)) %>% kable_styling(bootstrap_options = "striped", full_width = F) ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point() ``` I tried following the solution here but to no avail. 回答1: A great

Independent alpha for each plot within facet_grid based on density

本秂侑毒 提交于 2021-02-08 04:36:38
问题 I am constructing a facet_grid using stat_hexbin however I would like the alpha value to be independent for each of the facet plots. I am currently using the following code: ggplot (data, aes (x, y , fill = z)) + stat_binhex(bins=20, aes(alpha = ..count..)) + facet_grid(. ~ z) + guides(alpha = F) + coord_equal() + theme_bw() which produces the following plot: However, the alpha value, which is defined by ..count.. doesn't work when applied outside of the aes within stat_binhex . I would like

Memory leakage in using `ggplot` on large binned datasets

泪湿孤枕 提交于 2021-02-08 03:57:15
问题 I am making various ggplot s on a very large dataset (much larger than the examples). I created a binning function on both x- and y-axes to enable plotting of such large dataset. In the following example, the memory.size() is recorded at the start. Then the large dataset is simulated as dt . dt 's x2 is plotted against x1 with binning. Plotting is repeated with different subsets of dt . The size of the ploted object is checked by object.size() and stored. After the plotting objects have been