I'm producing a bar chart for 1200 observations using ggplot2. Each of these observations has an error bar. There's also an average shown (using geom_line) for these observations overall.
I'm finding the running time is very slow (2 seconds) in comparison to less observations (e.g. if 500 or were used <1 second). Also, all observations must be a seperate bar.
I realise it doesn't sound like much time, but this time adds up overall for what I need to do - producing over 100 of these plots and knitting them to rmd file.
Below is a piece of code I've created to replicate the issue - this is using ggplot2 inbuilt diamonds dataset.
diamonds1 <- as.data.frame(mutate(diamonds, upper = x + 1.2, lower = x - 0.4)) diamonds2 <- diamonds1 %>% group_by(cut) %>% summarize(Mean = mean(x, na.rm=TRUE)) ChosenCut <- "VVS28451" diamonds3 <- left_join(diamonds1 ,diamonds2, by = c("cut" = "cut") ) %>% filter(cut == "Very Good") %>% mutate(ID = paste0(clarity,row_number() )) %>% mutate(CutType = case_when(ID==ChosenCut ~ ID, TRUE ~ " Other Cut"), CutLabel = ifelse(ID == ChosenCut, "Your Cut", "")) diamonds4 <- diamonds3[order(-xtfrm(diamonds3$CutLabel)),] diamonds4 <- diamonds4[1:1255,] DiamondCutChart = diamonds4 %>% ggplot(aes(x = reorder(ID, x), y = x)) + geom_bar(aes(fill=CutType), stat = "identity", width = 1) + geom_errorbar(aes(ymin = lower, ymax = upper)) + geom_text(aes(label = CutLabel), position = position_stack(vjust = 0.5), size = 2.7, angle = 90, fontface = "bold") + geom_line(aes(y = diamonds4$Mean), group = 1, linetype=2, colour = "#0000ff") + scale_fill_manual(values = c("#32572C", "#41B1B1")) + annotate("text", x = 1, y = diamonds4$Mean, hjust =0, vjust = -0.5, size = 3.2, colour = "#0000ff", label=paste0("Mean ",diamonds4$Mean)) + theme_classic()+ theme(axis.title.x=element_blank(), axis.title.y=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), legend.position = "top") + labs(fill = "") StartTime = Sys.time() DiamondCutChart EndTime = Sys.time() EndTime - StartTime
When running this, it takes around 2 seconds. I need this to be less than 1 second to be able to produce multiple plots and rmarkdown outputs in less overall time.
How can I reduce the time it takes to plot the graph from the piece of code?
Any help or pointing in the right direction is greatly appreciated.
https://stackoverflow.com/questions/65756639/how-to-improve-speed-of-ggplot-bar-chart-when-plotting-1000-points January 17, 2021 at 10:05AM
没有评论:
发表评论