![]() # add element_markdown to # this will parse the labels to add img # we're also decreasing the margin so that the logos/heads are close # to the columnsĪ = element_markdown (margin = margin (t = - 25, unit = "pt" ) ) ) # axis_plot ggsave ( "qbr-axis-img.png", axis_plot, Width = 0.4 ) + scale_color_identity (aesthetics = c ( "fill", "color" ) ) + geom_hline (yintercept = 0, color = "black", size = 1 ) + theme_minimal ( ) + scale_y_continuous (breaks = scales :: pretty_breaks (n = 10 ) ) + labs (x = NULL,Ī = element_text (size = 16, face = "bold" ) ) + theme ( ![]() Rank = as.integer ( rank ) ) %>% ggplot ( ) + geom_col ( aes ( # CHANGE IS HERE # apply label to x axis labels Tic ( ) axis_plot % mutate (label = link_to_img ( headshot_href ), Credit to Emil Hvitfeldt for the idea of using an img tag and the function we’re adapting to embed images. Note that again, while this looks like a HTML call it works anywhere you want to use ggplot2. Thus, to achieve our goal of embedding images, we can create an img HTML tag to embed an image rather than just format text. In short, ggtext provides a limited subset of markdown/ HTML/ CSS syntax as an interface to changing text in ggplot2. The ggtext site has all the capabilities and function details. The overall purpose of ggtext is to provide “improved text rendering support for ggplot2”. Here ggtext::geom_richtext() handles the proper scaling of the image without really any intervention from us! While geom_richtext() is very useful we’re sort of using it out of its normal context. However, there’s an even easier way thanks to ggtext! While this may seem like a bit of work, it’s a good habit to think explicitly about what size or ratio you want to save your plots out as (it can inform how big to make your text amongst other things). Width = 0.4 ) + scale_color_identity (aesthetics = c ( "fill", "color" ) ) + geom_vline (xintercept = 0, color = "black", size = 1 ) + theme_minimal ( ) + scale_x_continuous (breaks = scales :: pretty_breaks (n = 10 ) ) + labs (x = "\nQBR",Ī = element_text (size = 16, face = "bold" ),Ī = element_text (margin = margin (r = - 25, unit = "pt" ) ), X = qbr_total, y = fct_reorder ( name, qbr_total ), Side_plot % mutate (label = link_to_img ( headshot_href ), Plot.caption = element_markdown (size = 12 ),Īxis.text = element_text (size = 14, face = "bold" ),Ī = element_text (size = 16, face = "bold" ) ) Plot.subtitle = element_text (size = 16 ), Plot.title = element_text (face = "bold", size = 20 ), Width = 0.4 ) + scale_color_identity (aesthetics = c ( "fill", "color" ) ) + geom_hline (yintercept = 0, color = "black", size = 1 ) + theme_minimal ( ) + scale_x_continuous (breaks = c ( 1, seq ( 5, 30, by = 5 ) ), limits = c ( 0.5, 34 ) ) + scale_y_continuous (breaks = scales :: pretty_breaks (n = 10 ) ) + labs (x = NULL,Ĭaption = "**Data:** espnscrapeR | **Plot:** ) + theme ( Rank = as.integer ( rank ) ) %>% ggplot ( ) + geom_col ( aes (įill = team_color, color = team_alt_color ), boxplot(df, boxfill = NA, border = NA) #invisible boxes - only axes and plot areaīoxplot(df, xaxt = "n", add = TRUE, boxfill="red",īoxwex=0.25, at = 1:ncol(df) - 0.15) #shift these left by -0.15īoxplot(df, xaxt = "n", add = TRUE, boxfill="blue",īoxwex=0.25, at = 1:ncol(df) + 0.15) #shift to the right by +0.Basic_plot % mutate (label = link_to_img ( headshot_href ), With different data frames, it may be necessary to change this to subset for whichever columns contain the data you want to plot. Note that in the following, we use df to exclude the 1st (id) column from the values to plot. Then add the 2 traces in the following two statements. The 1st boxplot statement creates a blank plot. Using base graphics, we can use at = to control box position, combined with boxwex = for the width of the boxes. P <- p + geom_point(aes(y=value, group=Label), position = position_dodge(width=0.75)) ![]() # if you want color for points replace group with colour=Label P <- ggplot(data = df.m, aes(x=variable, y=value)) P <- p + guides(fill=guide_legend(title="Legend_Title"))Įdit 3: How to align geom_point() points to the center of box-plot? It could be done using position_dodge. P <- p + facet_wrap( ~ variable, scales="free") P + facet_wrap( ~ variable, scales="free")Įdit 2: How to add x-labels, y-labels, title, change legend heading, add a jitter? p <- ggplot(data = df.m, aes(x=variable, y=value)) Here's an implementation of that as well: p <- ggplot(data = df.m, aes(x=variable, y=value)) + Ggplot(data = df.m, aes(x=variable, y=value)) + geom_boxplot(aes(fill=Label))Įdit: I realise that you might need to facet. require(reshape2)ĭf df.m # pasting some rows of the melted ame ![]() Otherwise, what you have done seems to be okay. You should get your data in a specific format by melting your data (see below for how melted data looks like) before you plot.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |