0
  1. I print the plot with agg_png my prefered function, I have used it previously and works, but with the current chart do not work. When printing the chart it places the axis y where the x axis should be I want the bar chart with the coordinates fliped , Also the logos when printed on Rstudio they seem to look ok, but when exported to a PNG they look really big. Additionally, the label for the segment part is not center aligned I have tried with hjust and vjust to correct it but has not worked out.

I have used a mix of several functions , first I printed the logos with ggtextures, I saw some examples with ggtext but I do not know HTML so have been unable to print images with that package. I use ggplot2 to create the plot. Also I use a custom function for the theme to organize a little bit the code.

library(tidyverse)
library(ggthemes)
library(scales)
library(ggtext)
library(ragg)    
library(systemfonts)   
library(magick)
library(png)
library(ggtextures)



years_vec <- c(5,5,5,4,4,3,3,2,3,2)
value_vec <- c(230,230,230,200,180,150,120,100,100,80)
name_vec <- c("Richard","Anthony","Desmond","Fred","John","Steven","Jeremy","Damon",
              "Brad","Alfred")
value_yr <- round(value/years,1)
data_df <- tibble(name=name_vec,
                  value=value_vec,
                  years=years_vec,
                  value_years=value_yr)

# these logos are logos from internet I would like to add some links for the logos but I have not figured out how to insert them. the logos can be any logos.
image_icons <- list(image_read("logo1.png"),
                    image_read("logo2.png"),
                    image_read("logo3.png"),
                    image_read("logo4.png"),
                    image_read("logo5.png"),
                    image_read("logo6.png"),
                    image_read("logo7.png"),
                    image_read("logo8.png"),
                    image_read("logo9.png"),
                    image_read("logo10.png")
                    
                    
)

# this logo is for the bottom right
logo <- image_read("logo11.png")


# custom function
theme_bar <- function( ) {
  
  theme(
    panel.background = element_rect(fill ="lightblue"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.ticks.x  = element_blank(),
    axis.ticks.length.x = unit(0.15,"cm"), 
    plot.background = element_rect(fill="lightblue"),
    legend.background = element_rect(fill="lightblue"),
    legend.key = element_rect(fill="lightblue"),
    axis.title.x = element_blank(),
    axis.text = element_text(size=11, color= "black",family = "arial"),
    axis.text.y = element_text(hjust = 0,vjust = -0.8),
    axis.text.x = element_text(margin = margin(t=-16)),
    plot.title.position = "plot",
    plot.title = element_textbox(size=24,color="black",vjust=1,
                                 family = "arial",width = unit(1, "npc"),
                                 margin= margin(r=-80),
                                 lineheight = 1.3),
    plot.caption.position = "plot",
    plot.caption = element_textbox(size=10,color="black",margin= margin(t = 18,r=80),
                                   lineheight = 1.1,
                                   family = "montserrat",
                                   width = unit(1, "npc")),
    
    axis.title.y.right = element_blank(),
    axis.title.y.left = element_blank(),
    axis.line.x = element_blank(),
    plot.margin = unit(c(1,3,1,1),"cm"),
    legend.position = "none"
    
  )
}


### dataframe top 10
data_plot  <- data_df %>% 
  mutate(Image = image_icons) 


#
#
# Plot ####
tle <- "The biggest values of our dataset highlight the best perfomers of the month"
cap <- "Source:Internet"

# I use geom_isotype_col to add the logos to the bars
pl1 <- data_plot %>% 
  ggplot(aes(x=reorder(name,value),
             y=value,
             image=Image
  )) +
  geom_col(fill="blue",width = 0.7) +
  labs(title = tle,
       caption = cap
  ) +
  coord_flip() +
  geom_isotype_col(
    img_height = grid::unit(0.9, "null"), img_width = NULL,
    ncol = 1, nrow = 1, hjust = 0.97, vjust = 0.5
  ) +
  geom_col(position = "dodge",aes(y=value_years),
           fill = "gray",width = 0.7) +
  geom_text(aes(x=name,
                y=value,
                label=value),
            hjust =-0.4,
            color="black") + 
  geom_text(
    aes(x=name,
        y=value_years,
        label=value_years
    ),
    vjust=0.5,hjust=3.5
  ) +
  theme_bar() +
  scale_y_continuous(
    breaks = breaks_width(50),
    limits = c(0,300)) 

# I use this to add the logo which should be at the right bottom of the plot
pl2 <- pl1 + 
  annotation_custom(grid::rasterGrob(logo), 
                    xmin = 16, 
                    xmax = Inf, 
                    ymin = -Inf, 
                    ymax = -70) +
  coord_cartesian(clip = "off")



# I always use this package to export the pngs , it works and i prefer it over other packages but the exported plot has the coordinates flipped
agg_png("plot_exp3.png", width = 10, height = 10, units = "in", res = 300)
pl2
dev.off()

EXPECTED I want the size of the result image but with the coordinates not being flipped and the logos at the right position. Also the labels for the segment are not aligned to the center. and want to be able to plot another logo to the bottom right of the plot but it did not work. Expected squared plot

RESULT this image has the right size but the plot got exported with the coordinates flipped. Whe I print it on Rstudio it seems to look ok . I am unable to reduce the chart bar a little bit , sometimes i get an error for the size I tried adding a subtitle and it did not have enough space. The logos are printed too big. Result

LAST CHANGE I changed coord_cartesian(clip="off) to coord_flip(clip="off) and it fixed the issue with the position of the plot but still got the issues with the labels alignment and reduction of the plot to make room for a longer title and more space for the caption.

improved image

  • Concerning the issue with `coord_flip`: You are overwriting the coord_flip when using `coord_cartesian(clip = "off")`. Use `coord_flip(clip="off")` instead. – stefan Jul 10 '23 at 11:55
  • @stefan Great thank you for the tip, I changed it and it makes sense it worked. but still got the issues with the sizing of the plot and alignment of the labels for the segment and the text axis of the names also seem a little off and not centered as well. – Edwin Acuña Jul 10 '23 at 13:15

0 Answers0