I am trying to generate a bar graph in R, but as the y-axis values are very close, there is no difference in the graph.
Can someone help me?
This is the data I use to generate the chart.
rede <- c("Wifi", "Wifi(AB)", "Wifi(AB) + 4G(AB)", "Wifi(AB) + 4G(B)", "4G(AB)")
disp <- c(0.9981663483026838, 0.9979983253954591, 0.9983305230561498, 0.9981898613052699, 0.9980460877265795)
down <- c(16.062788868489800, 17.534669535778500, 14.624618028127900, 15.85681496583588, 17.116271515163100)
dados <- data.frame("Ref" = rede, "Disponibilidade" = disp, "Downtime" = down)
ggplot(dados) +
aes(
x = Ref,
fill = Disponibilidade,
weight = Disponibilidade
) +
geom_bar(position = "fill") +
scale_fill_viridis_c(option = "plasma", direction = 1) +
labs(
x = "Redes",
y = "Valores",
title = "Gráfico Disponibilidade"
) +
theme_minimal() +
theme(
plot.title = element_text(size = 18L,
face = "bold",
hjust = 0.5)
)
As others mentioned in the comments, it's unclear what exactly you are looking for, but you could do something like this (as #JonathanV.Solórzano recommended with using coord_cartesian).
library(ggplot2)
ggplot(dados) +
aes(
x = Ref,
y = Disponibilidade,
fill = Disponibilidade,
weight = Disponibilidade
) +
geom_col() +
scale_fill_viridis_c(option = "plasma", direction = 1) +
coord_cartesian(ylim = c(min(dados$Disponibilidade), max(dados$Disponibilidade))) +
labs(x = "Redes",
y = "Valores",
title = "Gráfico Disponibilidade") +
theme_minimal() +
theme(plot.title = element_text(size = 18L,
face = "bold",
hjust = 0.5))
Output
Alternative with geom_bar (which produces the same output as above)
ggplot(dados) +
aes(
x = Ref,
fill = Disponibilidade,
weight = Disponibilidade
) +
geom_bar() +
scale_fill_viridis_c(option = "plasma", direction = 1) +
coord_cartesian(ylim = c(min(dados$Disponibilidade), max(dados$Disponibilidade))) +
labs(x = "Redes",
y = "Valores",
title = "Gráfico Disponibilidade") +
theme_minimal() +
theme(plot.title = element_text(size = 18L,
face = "bold",
hjust = 0.5))
Related
I want to change the text size of my y axis descrption and center my plottitle.
Everything coded within the themes function is not being applied to my graph.
Where is the problem?
finalchart = ggplot(euall,
aes(day, cumulative_cases_of_14_days_per_100000 ,
group = countriesAndTerritories)) +
geom_bar(stat = "identity" ,
col = "white" ,
fill = "darkred") +
facet_wrap("countriesAndTerritories") +
geom_line(aes(y = rollmean(cumulative_cases_of_14_days_per_100000, 1,
na.pad = TRUE),
col = "pink"),
show.legend = FALSE) +
labs(title = "COVID infections in the European Union in September 2020" ,
x = "\nSeptember" ,
y = "Infections per 100'000\n" ,
caption = "source: https://opendata.ecdc.europa.eu/covid19/casedistribution/csv") +
theme(axis.text.y = element_text(size = 5) ,
axis.title.y = element_text(size = 10) ,
plot.title = element_text(hjust = 0.5)) +
theme_minimal()
finalchart
The problem is the order in which you call theme() and theme_minimal(). By calling theme_minimal() second your manual settings in theme() are overwritten.
library(ggplot2)
library(patchwork)
p1 <- ggplot(data = cars, aes(x = speed, y = dist)) +
geom_point() +
ggtitle("theme_minimal second") +
theme(title = element_text(size = 24, color = "red", face = "bold")) +
theme_minimal()
p2 <- ggplot(data = cars, aes(x = speed, y = dist)) +
geom_point() +
ggtitle("theme_minimal first") +
theme_minimal() +
theme(title = element_text(size = 24, color = "red", face = "bold"))
p1+p2
I have data that looks like this, df_Filtered:
Product Relative_Value
Car 0.12651458
Plane 0.08888552
Tank 0.03546231
Bike 0.06711630
Train 0.06382191
I want to make a bar plot of the data in GGplot2:
ggplot(df_Filtered, aes(x = Product, y = Relative_Value, fill = Product)) +
scale_y_continuous(labels = scales::percent) +
geom_bar(stat = "identity") +
theme_bw() +
theme(plot.background = element_rect(colour = "black", size = 1)) +
theme(legend.position = "none") +
theme(plot.title = element_text(hjust = 0.5))
labs(x ="Product", y = "Percentage of total sell", title = "Japan 2010") +
theme(panel.grid.major = element_blank())
How do i get rid of the decimals on the y-axis in the chart? So that it says 20 % instead of 20.0 %?
Use percent_format from the scales package to set accuracy to 1.
library(ggplot2)
library(scales)
ggplot(df_Filtered, aes(x = Product, y = Relative_Value, fill = Product)) +
scale_y_continuous(labels = percent_format(accuracy = 1)) +
geom_bar(stat = "identity") +
theme_bw() +
theme(plot.background = element_rect(colour = "black", size = 1)) +
theme(legend.position = "none") +
theme(plot.title = element_text(hjust = 0.5)) +
labs(x ="Product", y = "Percentage of total sell", title = "Japan 2010") +
theme(panel.grid.major = element_blank())
DATA
df_Filtered <- read.table(text = "Product Relative_Value
Car 0.12651458
Plane 0.08888552
Tank 0.03546231
Bike 0.06711630
Train 0.06382191",
header = TRUE, stringsAsFactors = FALSE)
scales::percent_format(accuracy = 2) doesn't allow manual breaks = c(0, 0.5, .10).
So, I have to create the manual function scale_y_continuous(breaks = c(0, 0.5, .10), labels = function(x) paste0(round(as.numeric(x*100)), "%")) .
I've created a heatmap on temperatures by city. ggplot orders cities by default, from Z-A, not what I want. How can I change the code so that the cities would be ordered A-Z, how overall x and y can be ordered in ggplot ?
ggplot(Cities, aes(x = Month, y = City, fill = AvgTemp, frame = City)) +
geom_tile(color = "white", size = 0.5) +
scale_fill_gradient(name = "Average Temperature",low = "blue", high = "red") +
coord_equal() +
labs(x = "Month", y = "", title = "Average Temp") +
theme_tufte() +
theme(axis.ticks = element_blank()) +
theme(axis.text = element_text(size = 15)) +
theme(plot.title = element_text(size = 15)) +
theme(legend.title = element_text(size = 10)) +
theme(legend.text = element_text(size = 10))
I've included a reorder in your aes(y=).
ggplot(data=Cities) +
geom_tile( aes(x=Month, y=reorder(City, AvgTemp, median, order=TRUE), fill = AvgTemp), color = "white", size = 0.5) +
scale_fill_gradient(name = "Average Temperature",low = "blue", high = "red") +
coord_equal() +
labs(x = "Month", y = "", title = "Average Temp") +
theme_tufte()
This question already has answers here:
Showing data values on stacked bar chart in ggplot2
(3 answers)
Closed 8 years ago.
I'm trying put label in geom_bar but I can't.
My code
df <- data.frame(
uni = rep(c("D","E","F","G","H"),3),
Var1 = factor(c(rep("A",5),rep("B",5),rep("C",5))),
Freq = c(53.6,50.0,48.5,50.0,56.2,23.2,18.5,27.7,20.0,14.3,23.2,31.5,23.8,30.0,29.6))
df$label = paste(round(df$Freq,0),"%", sep = "")
ggplot(data = df, aes(x = uni, y = Freq, fill = Var1)) +
geom_bar(stat = "identity",position = "fill", width = 1) +
scale_fill_brewer(palette = 3) + geom_text(aes(y = Freq, label = label, position ="identity", face = "bold", size = 1), hjust=0.5, vjust=0.5) +
xlab('') +
ylab('') +
labs(fill = '') +
ggtitle('Example') +
theme(axis.text.y = element_text(size=14,face="bold"), panel.background = element_blank(), plot.title = element_text(size = 20, colour = "black", face = "bold")) +
guides(size=FALSE)
By using ddply from the plyr pacakage, we can create a new variable based on the cumulative sums to get the correct position for each label:
library(plyr)
df <- data.frame(
uni = rep(c("D","E","F","G","H"),3),
Var1 = factor(c(rep("A",5),rep("B",5),rep("C",5))),
Freq = c(53.6,50.0,48.5,50.0,56.2,23.2,18.5,27.7,20.0,14.3,23.2,31.5,23.8,30.0,29.6))
df = ddply(df, .(uni), transform, labPosition = cumsum(Freq)-Freq/2)
df$label = paste(round(df$Freq,0),"%", sep = "")
ggplot(data = df, aes(x = uni, y = Freq, fill = Var1)) +
geom_bar(stat = "identity", width = 1) +
scale_fill_brewer(palette = 3) +
geom_text(aes(y = labPosition, label = label, position ="identity"), hjust=0.5, vjust=0.5, size = 1, face = "bold") +
xlab('') +
ylab('') +
labs(fill = '') +
ggtitle('Example') +
theme(axis.text.y = element_text(size=14,face="bold"), panel.background = element_blank(), plot.title = element_text(size = 20, colour = "black", face = "bold")) +
guides(size=FALSE)
This creates a new variable of the cumulative sum by group, and then subtracts the frequency itself divided by 2 to center it in the middle of that segment.
Just trying to fix this overlapped labeling:
My code:
values=c(164241,179670)
labels=c("Private", "Public")
colors=c("#cccccc", "#aaaaaa")
categoriesName="Access"
percent_str <- paste(round(graph$values / sum(graph$values) * 100,1), "%", sep="")
values <- data.frame(val = graph$values, Type = graph$labels, percent=percent_str )
pie <- ggplot(values, aes(x = "", y = val, fill = Type)) + geom_bar(width = 1) +
geom_text(aes(y = **val + 1**, **hjust=0.5**, **vjust=-0.5**, label = percent), colour="#333333", face="bold", size=10) +
coord_polar(theta = "y") + ylab(NULL) + xlab(NULL) +
scale_fill_manual(values = graph$colors) + labs(fill = graph$categoriesName) +
opts( title = graph$title,
axis.text.x = NULL,
plot.margin = unit(c(0,0,0,0), "lines"),
plot.title = theme_text(face="bold", size=14),
panel.background = theme_rect(fill = "white", colour = NA) )
print(pie)
Tried messing with the values marked with asterisks (** **) but haven't got anywhere.
Any help appreciated.
here is an example:
pie <- ggplot(values, aes(x = "", y = val, fill = Type)) +
geom_bar(width = 1) +
geom_text(aes(y = val/2 + c(0, cumsum(val)[-length(val)]), label = percent), size=10)
pie + coord_polar(theta = "y")
Perhaps this will help you understand how it work:
pie + coord_polar(theta = "y") +
geom_text(aes(y = seq(1, sum(values$val), length = 10), label = letters[1:10]))