How to erase x label(11-28~12-02) in R ggplot - r

E = ggplot(km_data_person, aes(x = date, y = km,fill = "")) +
theme_minimal() +
geom_bar(stat = "identity") +
scale_fill_brewer(palette ="Paired") +
xlab("") +
ylab("") +
theme(axis.text.y = element_text(size = 18)) +
theme(legend.text = element_text(size = 20)) +
theme(axis.text.x = element_text(size = 11)) +
geom_line(data = km_data_all,
aes(date, 기관평균, color = "기관평균"),
linetype = "solid",
size = 1, group = 1) +
scale_color_manual(values = c('기관평균' = '#0066CC')) +
labs(color = '') +
theme(legend.position = "bottom") +
guides(fill = guide_legend(title = "")) +
guides(fill = "none")
The following graph is drawn from the above R code.
If I want to erase the x label how should the code change to?

E = ggplot(km_data_person,aes(x=date, y=km,fill="")) +
theme_minimal()+
geom_bar(stat="identity") +
geom_line(data=km_data_all,
aes(date,기관평균,color="기관평균"),
linetype = "solid",
size=1,
group=1)+
scale_fill_brewer(palette ="Paired")+
xlab("") +
ylab("")+
theme(axis.text.y = element_text(size = 18),
legend.text=element_text(size=20),
axis.text.x = element_blank(),
legend.position = "bottom")+
scale_color_manual(values = c('기관평균' = '#0066CC')) + labs(color = '') +
guides(fill="none")

Related

Barplot - stacked ggplot percentage barplot starting value NOT 0%

I have a plot like this:
library(ggplot2)
library(reshape2)
library(ggh4x)
data <- data.frame(col1=c("Sample1","Sample2","Sample3","Sample4","Sample5","Sample6"),
col2=c(0.5,0.1,0.4,0.05,0.05,0.9),
col3=c(0.6,0.1,0.3,0.1,0.1,0.8),
col4=c(0.5,0.3,0.2,0.05,0.15,0.8),
col5=c("a","a","a","b","b","b"),
col6=c("c","c","c","c","c","c"))
data2 <- melt(data)
ggplot(data=data2, aes(x = variable, y = value, fill=col1))+
geom_bar(position="stack", stat="identity")+
scale_fill_manual(values=c("#e6194B","#ffe119","#f58231","#911eb4","#42d4f4","#bfef45")) +
scale_y_continuous(expand = c(0, 0),labels = scales::percent) +
facet_nested(~col6 + ~col5, scales = "free_x",space = "free_x",switch = "x") +
ggtitle("Title") +
theme_classic() +
theme(strip.text.y = element_text(angle=0),legend.position = "right",
legend.key.size = unit(0.4, 'cm'),
axis.line = element_line(colour = "black"),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
strip.placement = "outside",
strip.background = element_rect(color = "white", fill = "white"),
axis.title = element_blank()) +
guides(fill=guide_legend(title=NULL, ncol = 1)) +
xlab("X axis") +
ylab("Y axis")
Which creates a barplot like this:
Please take a look
My question is simple, how can I set y-axis starting value to 10% instead of 0% (without changing the code too much). All answers are greatly appreciated! (Similar questions are checked, without success...)
While in general not recommended for bar charts one option to "set" the starting point would be to set the limits via coord_cartesian:
library(ggplot2)
library(ggh4x)
ggplot(data = data2, aes(x = variable, y = value, fill = col1)) +
geom_bar(position = "stack", stat = "identity") +
scale_fill_manual(values = c("#e6194B", "#ffe119", "#f58231", "#911eb4", "#42d4f4", "#bfef45")) +
scale_y_continuous(expand = c(0, 0), labels = scales::percent) +
facet_nested(~ col6 + ~col5, scales = "free_x", space = "free_x", switch = "x") +
ggtitle("Title") +
theme_classic() +
theme(
strip.text.y = element_text(angle = 0), legend.position = "right",
legend.key.size = unit(0.4, "cm"),
axis.line = element_line(colour = "black"),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
strip.placement = "outside",
strip.background = element_rect(color = "white", fill = "white"),
axis.title = element_blank()
) +
guides(fill = guide_legend(title = NULL, ncol = 1)) +
xlab("X axis") +
ylab("Y axis") +
coord_cartesian(ylim = c(.1, NA))

can anyone tell me why my y-axis tick marks have disappeared? as well my legend has an a formatted into the boxes and I'm unsure of how to get rid of

p <- ggplot(data = JRdata, aes(x = reorder(tag,+occlusion19), y = occlusion19,
fill = IndividualSelectionMetagenomics,
colour = IndividualSelectionMetagenomics)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("white","red")) +
scale_colour_manual(values = c("black", "black")) +
geom_col() +
geom_text_repel(aes(label=ifelse(IndividualSelectionMetagenomics=="Y",paste0(tag),"")),
angle = 90, size = 10, nudge_y = 1) +
theme_bw() + labs(x = "ID number", y = "Portion of Stomata Occluded /10") +
theme(axis.title.x = element_text(size = 40), axis.title.y = element_text(size = 40)) +
theme(legend.text = element_text(size = 20)) +
theme(legend.title = element_text(size = 40)) +
theme(legend.position = c(.95, .95), legend.justification = c("right", "top")) +
theme(element_blank()) +
theme(axis.text.x = element_blank()) +
ggsave(plot = p, width = 40, height = 20, dpi = 300, limitsize = FALSE, filename = "not squished axis.pdf")

use if else within ggplot chunk to change colour palette

I'd like to be able to change the colour palette in ggplot2 boxplots, according to another variable data_origin.
This makes my boxplots, complete with legend:
library(hrbrthemes)
library(ggplot2)
library(reshape2)
library(tidyverse)
data_origin <- "airborne"
mytitle <- "something more than this"
legend_title <- "some words"
melted <- reshape2::melt(iris)
bp1 <- ggplot(melted, aes(x = variable, y = value, fill = Species)) +
geom_boxplot() +
theme_ipsum() +
scale_fill_brewer(palette = "Greens") +
theme(
legend.position = "bottom",
plot.title = element_text(size = 10)) +
theme(axis.text.x = element_blank()) +
ggtitle(mytitle) +
xlab("") +
ylab("") +
facet_wrap(~variable, scale = "free")
bp1
This however drops the legend completely and ignores the if else:
bp1 <- ggplot(melted, aes(x = variable, y = value, fill = Species)) +
geom_boxplot() +
theme_ipsum() +
scale_fill_brewer(legend_title, if (data_origin == "airborne" ) {palette = "Blues"} else {palette = "Greens"}) +
theme(
legend.position = "bottom",
# legend.title = legend_title,
plot.title = element_text(size = 10)) +
theme(axis.text.x = element_blank()) +
ggtitle(mytitle) +
xlab("") +
ylab("") +
facet_wrap(~variable, scale = "free")
bp1
Besides what #stefan suggested, there are two ways in which you can do this (that I know of). The first is using ifelse() (I moved the relevant part to the end):
data_origin <- "airborne"
bp1 <- ggplot(melted, aes(x = variable, y = value, fill = Species)) +
geom_boxplot() +
theme_ipsum() +
theme(
legend.position = "bottom",
# legend.title = legend_title,
plot.title = element_text(size = 10)) +
theme(axis.text.x = element_blank()) +
ggtitle(mytitle) +
xlab("") +
ylab("") +
facet_wrap(~variable, scale = "free") +
scale_fill_brewer(legend_title, palette = ifelse(
data_origin == "airborne",
"Blues",
"Greens"
))
bp1
The other one is to build the plot up in two steps:
data_origin <- "not airborne"
bp1 <- ggplot(melted, aes(x = variable, y = value, fill = Species)) +
geom_boxplot() +
theme_ipsum() +
theme(
legend.position = "bottom",
# legend.title = legend_title,
plot.title = element_text(size = 10)) +
theme(axis.text.x = element_blank()) +
ggtitle(mytitle) +
xlab("") +
ylab("") +
facet_wrap(~variable, scale = "free")
if (data_origin == "airborne") {
bp2 <- bp1 +
scale_fill_brewer(legend_title, palette = "Blues")
} else {
bp2 <- bp1 +
scale_fill_brewer(legend_title, palette = "Greens")
}
bp2
Created on 2021-08-01 by the reprex package (v2.0.0)

How to do a pie chart with two factor variables and % inside the plot

How can I transform this bar plot into a pie chart?
This is the bar plot I have:
This is the code I use to make the bar plot:
dados_gráfico_distrito <- dados_desde_2015 %>%
filter(!is.na(qsd_distrito_nascimento_rec)) %>%
group_by(anoletivo_cat) %>%
count(anoletivo_cat, qsd_distrito_nascimento_rec) %>%
mutate(pct = n / sum(n), pct_label = scales::percent(pct, accuracy=1))
dados_gráfico_distrito$qsd_distrito_nascimento_rec <- factor(dados_gráfico_distrito$qsd_distrito_nascimento_rec, levels = c("Other", "Porto", "Braga"))
ggplot(dados_gráfico_distrito, aes(x= anoletivo_cat, fill = qsd_distrito_nascimento_rec, y = pct)) +
geom_bar(position = "fill", stat="identity", width = 0.5) +
geom_text(aes(label = paste(pct_label), y = pct), position = position_fill(vjust = 0.5), colour = "black", size = 3.2) +
scale_y_continuous(labels = scales::percent) +
labs(y = " ", x = " ", fill=" ") +
theme_void() + scale_fill_brewer(palette="Paired") +
theme(legend.text = element_text(size = 8, colour = "black")) +
theme(axis.text = element_text(size = 8, colour = "black")) +
theme(legend.position = "bottom", legend.direction = "horizontal") +
guides(fill = guide_legend(reverse=TRUE)) +
theme(plot.margin = unit(c(1, 1, 1, 1), "cm")) +
theme(panel.grid = element_line(colour="grey90")) +
theme(panel.grid.minor.y = element_line(color = "white"), panel.grid.major.x = element_line(color = "white"))
When I try to transform it in a pie chart, adding the code line coord_polar () I get this chart:
This is what I pretend:
Thank you!
As you did not provide sample data, I have used some other sample data. Perhaps this will meet your needs. Please modify as necessary.
library(ggrepel)
library (ggplot2)
df = read.csv("https://www.dropbox.com/s/lc3xyuvjjkyeacv/inputpie.csv?dl=1")
df <- df %>% group_by(fac) %>%
mutate(
facc = ifelse(fac=="f1", "15/16 to 19/20", "20/21"),
cs = rev(cumsum(rev(per))),
text_yp = per/2 + lead(cs, 1),
text_yp = if_else(is.na(text_yp), per/2, text_yp)
) %>% data.frame()
df$type <- factor(df$type, levels=unique(df$type))
ggplot(df, aes(x="", y=per, fill=type )) +
geom_bar(stat="identity", width=1) +
coord_polar("y", start=0) +
facet_grid(facc~. ) +
scale_fill_brewer(palette="Paired") +
theme_void() +
geom_label_repel(
aes(label = text_y, y = text_yp), show.legend = FALSE
) +
scale_y_continuous(labels = scales::percent) +
labs(y = " ", x = " ", fill=" ") +
theme(legend.text = element_text(size = 8, colour = "black")) +
#theme(axis.text = element_text(size = 8, colour = "black")) +
#theme(legend.position = "bottom", legend.direction = "horizontal") +
guides(fill = guide_legend(reverse=TRUE)) +
theme(plot.margin = unit(c(1, 1, 1, 1), "cm")) +
theme(panel.grid = element_line(colour="grey90")) +
theme(panel.grid.minor.y = element_line(color = "white"), panel.grid.major.x = element_line(color = "white"))

ggplot no grid visual on top of imported image

I'm trying to plot some data on top of an imported image using ggplot2. However I can not get a visual grid in the plot..
Here is a simplified example to illustrate my issue:
library(ggplot2)
library(scales)
library(jpeg)
library(scales)
library(grid)
#picture from internet
myurl <- "http://upload.wikimedia.org/wikipedia/commons/9/95/Apollonian_spheres.jpg"
z <- tempfile()
download.file(myurl,z,mode="wb")
pic <- readJPEG(z)
file.remove(z) # cleanup
x <- sample(1:10, replace=T, 10)
y <- c("a","b","c","d","e","f","g","h","i", "j")
df <- data.frame(y,x)
p <-ggplot(df, aes(y, x, fill=y)) +
annotation_custom(rasterGrob(pic, width=unit(1,"npc"), height=unit(1,"npc")),
-Inf, Inf, -Inf, Inf) +
geom_bar(stat = "identity", fill="darkorange",width=0.8, alpha=0.75 )+
#geom_text(aes(label=data2$Attributes), vjust=1.5,colour="black")
coord_flip() + ggtitle("Something")+ theme_classic() +
labs(y = "yyy", x = "xxx") + guides(fill = guide_legend(reverse=TRUE))+
theme(axis.text.y = element_blank()) + theme(plot.title = element_text(size=20))+
theme(axis.title.x = element_text(size = 16))+ theme(axis.title.y = element_text(size = 16))+
theme(legend.text = element_text( size = 14)) + theme(legend.title = element_text( size = 16))+
theme(panel.grid.major = element_line(colour = "white", linetype = "dotted"))
p
#without picture works fine
p <-ggplot(df, aes(y, x, fill=y)) +
geom_bar(stat = "identity", fill="darkorange",width=0.8, alpha=0.75 )+
#geom_text(aes(label=data2$Attributes), vjust=1.5,colour="black")
coord_flip() + ggtitle("Something")+ theme_classic() +
labs(y = "yyy", x = "xxx") + guides(fill = guide_legend(reverse=TRUE))+
theme(axis.text.y = element_blank()) + theme(plot.title = element_text(size=20))+
theme(axis.title.x = element_text(size = 16))+ theme(axis.title.y = element_text(size = 16))+
theme(legend.text = element_text( size = 14)) + theme(legend.title = element_text( size = 16))+
theme(panel.grid.major = element_line(colour = "black", linetype = "dotted"))
p
Any ideas?

Resources