ggplot2 geom_text position in pie chart - ggplot2

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)

Related

Plotly does not properly show axis numbers with math_format

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)

Surface plotting with ggplot2

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

Bar plot in ggplot2

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

Manually change bar width using ggplot2

I've been trying to change the bars width and I'm using geom_bar (width) but it does not change the bars width, I need to make them more narrow
library(ggplot2)
library(tidyverse)
color_table <- tibble(
Land_cover = c("A", "B", "C", "D"),
Color = c("yellow", "darkgreen", "blue4", "maroon3")
)
df <- data.frame(
name=c("FM_BICEP","FM_NR","FM_TRICEP","FM_H_GRASP1","FM_CS_SPE","FM_MOS_SFL","FM_H_GRASP3*","FM_FS_RET","FM_W_SE3","FM_FS_ABD*","FM_MOS_SAB") ,
value=c(1.7,1.8,1.8,22.0,26.8,27.4,27.9,31.8,33.4,35.8,35.8),
group=c("A","A","A","C","D","A","C","A","B","A","A")
)
df$name <- factor(df$name, levels = df$name)
df$group <- factor(df$group, levels = color_table$Land_cover)
# Barplot
ggplot(df, aes(x=name, y=value,fill = group)) +
geom_bar(stat = "identity", aes(fill=group))+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+
scale_fill_manual(values = color_table$Color) + geom_col(width = 0.4)
I recommend you to use geom_col() as you use both x and y axis. It's the same as geom_bar(scale = "identity").
Also, to width work, I moved it to inside the geom_bar() function.
library(ggplot2)
library(tidyverse)
color_table <- tibble(
Land_cover = c("A", "B", "C", "D"),
Color = c("yellow", "darkgreen", "blue4", "maroon3")
)
df <- data.frame(
name=c("FM_BICEP","FM_NR","FM_TRICEP","FM_H_GRASP1","FM_CS_SPE","FM_MOS_SFL","FM_H_GRASP3*","FM_FS_RET","FM_W_SE3","FM_FS_ABD*","FM_MOS_SAB") ,
value=c(1.7,1.8,1.8,22.0,26.8,27.4,27.9,31.8,33.4,35.8,35.8),
group=c("A","A","A","C","D","A","C","A","B","A","A")
)
df$name <- factor(df$name, levels = df$name)
df$group <- factor(df$group, levels = color_table$Land_cover)
# Barplot
ggplot(df, aes(x=name, y=value,fill = group)) +
geom_col(aes(fill=group),
width = 0.4)+
scale_fill_manual(values = color_table$Color)+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
Created on 2022-11-19 with reprex v2.0.2

ggplot legend vertical AND horizontal

ggplot(mtcars, aes(x = mpg,
y = wt,
size = hp,
colour = as.factor(cyl))) +
geom_point() +
theme(legend.direction = "vertical",
legend.box = "horizontal",
legend.position = "bottom")
gives me
How can I produce the legend in a way, where the cyl-label remains vertical and the hp categories are arranged horizontally?
You can individually control legends via guides(...):
ggplot(mtcars, aes(x = mpg,
y = wt,
size = hp,
colour = as.factor(cyl))) +
geom_point() +
theme(legend.direction = "vertical",
legend.box = "horizontal",
legend.position = "bottom") +
guides(size=guide_legend(direction='horizontal'))