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
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)
Is it possible to plot with ggplot2 3D surface which is presented as (x, y, z)-vector with labeled countour lines?
Desired result is presented below
Surface map with countour lines
This is exactly what the geomtextpath package was built for.
Example copied from ?geomtextpath::geom_textcontour
library(geomtextpath)
#> Loading required package: ggplot2
df <- expand.grid(x = seq(nrow(volcano)), y = seq(ncol(volcano)))
df$z <- as.vector(volcano)
ggplot(df, aes(x, y, z = z)) +
geom_contour_filled(bins = 6, alpha = 0.6) +
geom_textcontour(bins = 6, size = 2.5, padding = unit(0.05, "in")) +
scale_fill_manual(values = terrain.colors(11)) +
theme_classic() +
theme(legend.position = "none")
Created on 2023-01-26 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 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)