I would like to ask how to handle ouptut from dplyr and then plot if in ggplot as geom_bar
Using the code below will give as plot, however It does not have right properties:
library(dplyr)
library(ggplot2)
mtcars %>% select(-cyl) %>%
group_by(gear) %>%
summarise(mean_hp = mean(hp),
median_hp = median(hp),
count = n()) %>%
gather() %>%
ggplot(aes(value)) + geom_bar() +
facet_wrap(~key, scales = "free")
Here you want to set both x and y aesthetics:
mtcars %>% select(-cyl) %>%
group_by(gear) %>%
summarise(mean_hp = mean(hp),
median_hp = median(hp),
count = n()) %>%
gather() %>%
ggplot(aes(x=key, y=value)) + geom_col()
Note that geom_col is better adapted in your case, as geom_bar is used for histograms (the height of the bar is proportional to the number of cases in each group).
You could still use faceting anyway but with only one plot per facet, the result will not be that pretty.
On second thought, it seems that your example makes little sense to me. Wouldn't this be more usefull?
mtcars %>% select(-cyl) %>%
group_by(gear) %>%
summarise(mean_hp = mean(hp),
median_hp = median(hp),
count = n()) %>%
pivot_longer(cols=c(mean_hp, median_hp)) %>%
ggplot(aes(x=name, y=value, fill=factor(gear))) +
geom_col(position = "dodge")
(Also, try to use pivot_longer and pivot_wider instead of gather and spread, which are now deprecated)
Related
The code below indicates that, while using math_format command in ggplot 'labels', the plot displays well if ggplot is used, but it fails if it is displayed through plotly. I need to use plotly in my code. Does somebody have some suggestion?
library(tidyverse)
library(scales)
library(plotly)
p <- mtcars %>% ggplot(aes(x=mpg, y=disp))+
geom_point() +
scale_x_continuous(trans = log_trans(),
breaks = trans_breaks("log", function(x) exp(x), n.breaks = 5),
labels = trans_format("log", math_format(e^.x, format = function(x) number(x, accuracy = 0.01, decimal.mark = ','))))
p
ggplotly(p)
Will anybody help develop a bar plot using ggplot2 for this data:
I am unable to develop a barplot for this data.
In ggplot you should make your data in a longer format. For this you could use pivot_longer from tidyr. With that you could create a stacked barplot like this:
df <- data.frame(Valley = c("Hushey", "Kanday", "Thallay"),
Female = c(144, 43, 45),
Young = c(160, 43, 22),
Yearling = c(162, 20, 25))
library(tidyr)
library(dplyr)
library(ggplot2)
df %>%
pivot_longer(cols = -Valley) %>%
ggplot(aes(x = Valley, y = value, fill = name)) +
geom_col()
Or make a facet plot using facet_wrap like this:
df %>%
pivot_longer(cols = -Valley) %>%
ggplot(aes(x = name, y = value)) +
geom_col() +
facet_wrap(~Valley)
Created on 2023-01-23 with reprex v2.0.2
I have a question regarding markdown and patchwork. Let's say I combined figures FigureLength and FigureWidth to make CombinedFigure through patchwork (two figures on top of each other):
library(datasets)
library(patchwork)
data(iris)
FigureLength <- iris %>%
ggplot(aes(x = Species, y = Sepal.Length)) +
geom_boxplot()
FigureWidth <- iris %>%
ggplot(aes(x = Species, y = Sepal.Width)) +
geom_boxplot()
CombinedFigure <- FigureLength / FigureWidth
Both figures nicely on top of each other in R
save(CombinedFigure, file = here::here ("CombinedFigure"))
In Markdown when I put the following only Sepal Length appears in my word document and not both figures on top of each other.
```{r CombinedFigure, dpi = 300, echo = FALSE}
CombinedFigure
```
What appears in markdown - only one figure
Thanks for your help.
When I run the following code:
---
title: "Untitled"
author: "Author"
date: "4/4/2022"
output: word_document
---
```{r}
library(datasets)
library(tidyverse)
library(patchwork)
data(iris)
FigureLength <- iris %>%
ggplot(aes(x = Species, y = Sepal.Length)) +
geom_boxplot()
FigureWidth <- iris %>%
ggplot(aes(x = Species, y = Sepal.Width)) +
geom_boxplot()
CombinedFigure <- FigureLength / FigureWidth
```
```{r CombinedFigure, dpi = 300, echo = FALSE}
CombinedFigure
```
I to get both figures nicely on top of each other in WORD like this:
I am plotting pie charts with ggplot2 and succeeded in having the percentage-labels centered in each slice
library(dplyr)
library(ggplot2)
library(ggpubr)
library("readxl")
df <- read_excel("Radiocomp.xlsx")
df$Pattern <- factor(cc$Pattern)
str(cc)
GGO <- ggplot(data=df, aes(x = "", y = GGO, fill = Pattern)) +
geom_bar(stat="identity", color = "white") +
geom_text(aes(label = paste0(GGO, "%")), position = position_stack(vjust = 0.5)) +
coord_polar("y") +
theme_void()
GGO
Pie chart
I try to place the percent-label outside the pie for better readability
Any recommendation?
Thank you
This can be achieved by setting the x aesthetic inside geom_text, e.g. x = 1.6 will put the label just outside of the pie.
library(ggplot2)
library(dplyr)
# example data
mpg1 <- mpg %>%
count(class) %>%
mutate(pct = n / sum(n))
ggplot(mpg1, aes(x = "", y = pct, fill = class)) +
geom_bar(stat = "identity", color = "white") +
geom_text(aes(x = 1.6, label = scales::percent(pct, accuracy = .1)), position = position_stack(vjust = .5)) +
coord_polar("y") +
theme_void()
Created on 2020-06-03 by the reprex package (v0.3.0)
I would like to plot something that is "between" a histogram and a density plot. Here is an example:
library(ggplot2)
set.seed(1)
f1 <- rep(1, 100)
v1 <- rnorm(100)
df1 <- data.frame(f1, v1)
f1 <- rep(2, 10)
v1 <- (rnorm(10)+1*2)
df2 <- data.frame(f1, v1)
df <- rbind(df1, df2)
df$f1 <- as.factor(df$f1)
ggplot(df, aes(x = v1, colour = f1)) +
geom_density(position="identity", alpha = 0.6, fill = NA, size = 1)
You will see that the area under each curve is 1.0, which is OK for a density. BUT notice that the second distribution is made up of just 10 observations rather than the 100 of the first. What I would like is that the area under curve 2 reflects this, e.g. is a tenth of that of curve 1. Thanks.
There is a computed variable for stat_density that you can use, called count.
ggplot(df, aes(x = v1, colour = f1)) +
geom_density(position="identity", alpha = 0.6, fill = NA, size = 1,
aes(y = after_stat(count)))
Note for ggplot2 <3.3.0 use stat(count) instead of after_stat(count).
You can find these tricks in the documentation of ?geom_density() under the section "Computed Variables".