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
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'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
I am using the code below to generate this heatmap:
dim = np.arange(1, 32, 1)
fig, ax = plt.subplots(figsize=(7,9))
heatmap = ax.imshow(h.T, cmap=plt.cm.get_cmap('Blues', 4), clim=[1,144])
cbaxes = fig.add_axes([.8, .35, .04, .3])
cbar = fig.colorbar(heatmap, ticks = [1, 36, 72, 108, 144], label = 'Number of valid records per day', cax = cbaxes)
ax.set_ylabel("Days", fontsize=15)
ax.set_xlabel("Months", fontsize=15)
ax.set_title("Number of valid records per day", fontsize=20)
ax.set_yticks(range(0,31))
ax.set_yticklabels(dim, ha='center', minor=False, fontsize=12)
ax.set_xticks(range(0,13,1))
ax.set_xticklabels(ylabel, rotation = 45, ha = 'right')
ax.set_facecolor('gray')
cbar.set_label('Number of valid records')
ax.xaxis.set_minor_locator(MultipleLocator(0.5))
ax.yaxis.set_minor_locator(MultipleLocator(0.5))
ax.tick_params(axis='y', which='major', pad=10)
ax.grid(which = 'minor', color = 'w')
fig.show()
As you can see there is a slight offset of the gridlines with respect to the heat map cells. Why is that? How can I fix it?
Thanks to the comment left by Jody Klymak, I added the following line of code at the beginning of my notebook and it solved the problem:
matplotlib.rcParams['figure.dpi'] = 300
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)