I am plotting my data and want to display totals on top of each bar but as soon as I add total count the bars disappear.
long<- data.frame(
Name = c("abc","abc","abc","gif","gif","gif","xyz","xyz","xyz"),
variable = c("a","b","c","a","b","c","c","b","a"),
value = c(4,6,NA,2,8,1,6,NA,NA))
Code
p<-long %>%
ggplot() + aes(Name, value, fill=variable) +
geom_bar(stat="summary", position = "fill") +
scale_y_continuous(labels = scales::percent_format()) +
ylab("Total_num") +
ggtitle("Totalnum") +
theme(plot.title = element_text(size = 20, hjust = 0.5)) +
theme(axis.text.x = element_text(angle = 75, vjust = 0.95, hjust=1))
p+ stat_summary(fun.y = sum, aes(label = ..y.., group = Name)+
geom_text(stat='value', aes(group=Name, label=.."value"..), position = position_stack(vjust = 0.5))
You can achieve that creating another df with the sum of value per Name and passing this to geom_text()
long<- data.frame(
Name = c("abc","abc","abc","gif","gif","gif","xyz","xyz","xyz"),
variable = c("a","b","c","a","b","c","c","b","a"),
value = c(4,6,NA,2,8,1,6,NA,NA))
long_totals <- long %>%
group_by(Name) %>%
summarise(Total = sum(value, na.rm = T))
p <- ggplot()+
geom_bar(data = long,
aes(x = Name,
y = value,
fill=variable),
stat="summary",
position = "fill") +
geom_text(data = long_totals,
aes(y = 100,
x = Name,
label = Total),
size = 7,
position = position_fill(vjust = 1.02)) +
scale_y_continuous(labels = scales::percent_format()) +
ylab("Total_num") +
ggtitle("Totalnum") +
theme(plot.title = element_text(size = 20, hjust = 0.5)) +
theme(axis.text.x = element_text(angle = 75, vjust = 0.95, hjust=1))
Related
Here is my data which produces a heat map. What I am hoping to do is produce multiple difference heatmaps with an outline around each of x categories.
data <- data.frame(id=c("john","john","john","kate","kate","kate","chris","chris","chris"),
group=c("geo","his","math","geo","his","math","geo","his","math"),
grade=c(65,76,87,67,89,98,99,97,96),
class=c("A","A","A","A","A","A","B","B","B"))
data
mine.heatmap <- ggplot(data = data, mapping = aes(x = id, y = group, fill = grade)) +
geom_tile() +
xlab(label = "id") +
ylab(label="group") +
labs(fill="grade")+
scale_fill_gradient2(low = "#800080",
high = "#FF8C00",mid = "white")
x <- mine.heatmap + facet_grid(
cols = vars(class), scales = "free", space = "free"
)
x + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1, size = 18, margin = margin(b=2)))+
theme(axis.text.y= element_text(angle = 0, vjust = 0.5, hjust=1, size = 18)) +
theme(legend.text = element_text(size=14))+
theme(legend.title = element_text(size=14))+
theme(strip.text = element_text(size=14))+
theme(axis.title.x = element_text(size=18)) +theme(axis.title.y = element_text(size=18))
Original Heat map:
What I am hoping to get are the following heatmaps:
One option to achieve your desired result would be to
put your plotting code in a function which takes as one argument the id for which you want to draw a outline.
Use some data wrangling to convert the categories to be plotted on the x and y aes to numerics per facet variable.
Add a geom_rect to your plotting code to draw the outline which uses the numerics computed in step 2.
library(ggplot2)
library(dplyr)
mine_heatmap <- function(x) {
p <- ggplot(data = data, mapping = aes(x = id, y = group, fill = grade)) +
geom_tile() +
# Add outline via a geom_rect
geom_rect(
data = subset(data, id == x),
aes(
xmin = id_num - .5, xmax = id_num + .5,
ymin = min(group_num) - .5, ymax = max(group_num) + .5
), fill = NA, color = "black", size = 1
) +
labs(x = "id", y = "group", fill = "grade") +
scale_fill_gradient2(
low = "#800080",
high = "#FF8C00", mid = "white"
)
p <- p + facet_grid(
cols = vars(class), scales = "free", space = "free"
)
p + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1, size = 18, margin = margin(b = 2))) +
theme(axis.text.y = element_text(angle = 0, vjust = 0.5, hjust = 1, size = 18)) +
theme(legend.text = element_text(size = 14)) +
theme(legend.title = element_text(size = 14)) +
theme(strip.text = element_text(size = 14)) +
theme(axis.title.x = element_text(size = 18)) + theme(axis.title.y = element_text(size = 18))
}
# Convert id and group to numerics per facet variable
data <- data |>
group_by(class) |>
mutate(
id_num = as.numeric(factor(id)),
group_num = as.numeric(factor(group))
) |>
ungroup()
mine_heatmap("john")
mine_heatmap("kate")
mine_heatmap("chris")
I am having issues positionning my labels for my grouped bar chart.
Below a reproducible example, and on the image you can see where I would like the labels to be placed.
Not all on the same line, but each label aligned with each of the 3 bar chart, and ideally each label placed right above each corresponding bar chart.
Any idea how to achieve this? I want to keep a grouped bar chart and not a stacked bar chart.
data_F <- "https://raw.githubusercontent.com/max9nc9/Temp/main/temp.csv"
data_F <- read.csv(data_F, sep = ";")
colnames(data_F) <- c("Month_Year", "Type", "Amount")
data_F$Amount <- as.numeric(data_F$Amount)
Final_Graph <-
ggplot(data_F, aes(x = Month_Year, y = Amount, label = Amount, fill = Type)) +
geom_bar(position="dodge", stat = "identity") +
geom_text(size = 3, position = position_stack(vjust = 0.5), color = "black") +
theme(axis.text.x = element_text(angle = 90)) +
scale_fill_manual(values = c("#e62200", "#00c41d", "#f7b21e")) +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.subtitle = element_text(hjust = 0.5))
Final_Graph
As you use position_dodge for the bars use it for the labels as well, where I use width = .9, which is the default for bars:
data_F <- "https://raw.githubusercontent.com/max9nc9/Temp/main/temp.csv"
data_F <- read.csv(data_F, sep = ";")
colnames(data_F) <- c("Month_Year", "Type", "Amount")
data_F$Amount <- as.numeric(data_F$Amount)
library(ggplot2)
ggplot(data_F, aes(x = Month_Year, y = Amount, label = Amount, fill = Type)) +
geom_bar(position="dodge", stat = "identity") +
geom_text(size = 3, position = position_dodge(width = .9), vjust = 0, color = "black") +
theme(axis.text.x = element_text(angle = 90)) +
scale_fill_manual(values = c("#e62200", "#00c41d", "#f7b21e")) +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.subtitle = element_text(hjust = 0.5))
Using stat_summary function for getting the sum of each bar and plotting it on to stack bar chart.
It is working fine, but somehow it is disturbing the "Aging" table on the right. It is not in order.
When I remove the stat_summary code line, it's showing me in order. But I want to show the total of each bar; therefore, I used the stat_summary function.
df3 <- group_by(df, AgeGroup, Org_Name)
totalbarsum <- aggregate(df3$ClaimValue_INR, by=list(Category=df3$Org_Name), FUN=sum)
dfsecondgraph <- totalsumorg
valueorg <- format(dfsecondgraph$x,big.mark=",",scientific=FALSE)
dfsecondgraph$Aging <- dfsecondgraph$Group.2
datareordersecond <- dfsecondgraph
ggplot(datareordersecond,aes(x = reorder(Category, x,FUN = sum), y = x, fill = Aging))+
geom_bar(stat = "summary", fun = sum, position = position_stack())+
geom_col(position = position_stack(reverse = TRUE))+
geom_text(size = 3, position = position_stack(vjust = 0.5,reverse = TRUE), check_overlap = T,label = valueorg) +
stat_summary(fun = sum, aes(label = scales::comma (..y..), group = Category),size = 4,vjust = -1, hjust = 0,fontface = "bold",geom = "text")+
xlab("PAN India Dealerships ") +
ylab("Claim Value INR") +
ggtitle("Pending Claims Status : PAN India Dealerships ")+theme(plot.title = element_text(lineheight=3, face="bold", color="black", size=20))+
scale_y_continuous(labels = comma)+
theme(axis.text.x = element_text(angle = 0, hjust = 1, size=15))+
theme(axis.text.y = element_text(angle = 0, hjust = 1, size=15))+
theme(axis.title.y = element_text(size = rel(1.8), angle = 90))+
theme(axis.title.x = element_text(size = rel(1.8), angle = 00))+
scale_fill_manual(values = c("#F8766D", "#7CAE00", "#00BFC4","#C77CFF","#F0E442","#f37735"))+
coord_flip()
I am trying to make the y axis of the second plot over 2 lines. Using '\n' for the first plot worked fine but using it on the second makes the text in odd places (maybe because of the italics).
p1 <- ggplot(data = new_data) +
geom_line(mapping = aes(x = Date,
y = Proportion,
group = Species,
colour = Species)) +
scale_colour_manual(values=c(Golden_Trevally="goldenrod2",
Red_Snapper="firebrick2",
Sharksucker_Remora="darkolivegreen3",
Juvenile_Remora="aquamarine2")) +
xlab("Date (2014-2018)") +
ylab("Total Presence \n Per Month ") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
theme(legend.position="top") +
labs(colour = "Hitchhiker Species")
new_data_counts <- new_data %>% select(Date, Count)
new_data_counts <- new_data_counts[!duplicated(new_data_counts),]
p2 <- ggplot(data = new_data_counts) +
geom_bar(mapping = aes(x = Date, y = Count), stat = 'identity') +
xlab("Date (2014-2018)") +
ylab("Total Number of "~italic(\nM.alfredi)~" Encounters") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
grid.arrange(p1,p2)
You can try this:
geom_line(mapping = aes(x = Date,
y = Proportion,
group = Species,
colour = Species)) +
scale_colour_manual(values=c(Golden_Trevally="goldenrod2",
Red_Snapper="firebrick2",
Sharksucker_Remora="darkolivegreen3",
Juvenile_Remora="aquamarine2")) +
xlab("Date (2014-2018)") +
ylab("Total Presence \n Per Month ") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
theme(legend.position="top") +
labs(colour = "Hitchhiker Species")
new_data_counts <- new_data %>% select(Date, Count)
new_data_counts <- new_data_counts[!duplicated(new_data_counts),]
p2 <- ggplot(data = new_data_counts) +
geom_bar(mapping = aes(x = Date, y = Count), stat = 'identity') +
labs(x="Date (2014-2018)",
y=expression(atop(paste("Total Number of"), paste(italic("M.alfredi"), " Encounters")))) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
grid.arrange(p1,p2)
You need space between \n and M.alfredi in p2. Since there is no reproducible example, here is my suggestion for the second plot,
p2 <- ggplot(data = new_data_counts) +
geom_bar(mapping = aes(x = Date, y = Count), stat = 'identity') +
xlab("Date (2014-2018)") +
ylab("Total Number of "~italic(\n M.alfredi)~" Encounters") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
It should solve your problem.
My codes is here
Yr = c("2016","2017","2016","2017","2016","2017")
Type = c("A","A","B","B","C","C")
Value = c(73,183,160,476,11,73)
Percentage = c(29.92,25.00,65.57,65.03,4.51,9.97)
p1Data <- data.frame(Yr,Type,Value,Percentage)
library(ggplot2)
p1 <- ggplot(p1Data, aes(Type, Value, fill = Yr)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +
theme(axis.title.x = element_blank(), plot.title = element_text(hjust = 0)) +
geom_text(aes(label = paste(Value, paste(Percentage, "%"), sep = "\n"), y = Value), size = 4, vjust = 1.5, position = position_dodge(width = 0.9)) +
ggtitle("2016 V.S. 2017") +
labs(fill = "Catagory")
You could change vjust = 1.5 to vjust = "inwards" in your geom_text.
ggplot(p1Data, aes(Type, Value, fill = Yr)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +
theme(axis.title.x = element_blank(), plot.title = element_text(hjust = 0)) +
geom_text(aes(label = paste(Value, paste(Percentage, "%"), sep = "\n"),
y = Value),
size = 4,
vjust = "inward",
position = position_dodge(width = 0.9)) +
ggtitle("2016 V.S. 2017") +
labs(fill = "Catagory")