0

I have the following dataset. Note that these are fictitious values:

Farm    CO2Fert CO2Manure   CO2Feed CO2Housing  CO2Fuel CO2Other
Best    2.187635996 0.670289261 0.773605214 2.415801361 1.180859203 2.876050311
Worst   4.240789564 0.503797793 1.884548328 5.114791701 3.411847194 5.150085802
User    1.189743632 2.852713125 0.419821994 0.630429463 2.982960729 1.489036959

I have been asked to draw a spider diagram of these values. Having looked at fmsb and ggradar I have for a number of reasons decided to start from scratch instead. So, I have the following code (not currently sure if all the libraries are necessary):

library(dplyr)
library(ggplot2)
library(scales)
library(reshape2)
library(tibble)
library(data.table)

data <- fread("C:/myData/ExampleFootprintData.csv")
dat2test <- melt.data.table(data, c("Farm"), c("CO2Fert", "CO2Manure", "CO2Feed", "CO2Fuel", "CO2Housing", "CO2Other"))

thePlot <-  ggplot(data = dat2test, aes(x=variable, y=value, group=Farm, color= Farm)) + ## Define data and colour column
  geom_polygon(fill = NA) + ## make the lines meet
  coord_polar() ## Make it round

The chart appears as follows: Image of simple spider diagram

As you can see the labels are not fully onto the plot surface. Looking at the examples around on the internet (e.g. the main text I've taken my code from) this appears not to be addressed anywhere. There is an interesting solution to this here, and I am considering using it, but my eventual labels are likely to be long and descriptive. I would like them to be shown horizontally as they are now with line breaks as appropriate, but so that they fit on the chart.

So far, I have not got very far with this. using theme(axis.title.x = element_text(vjust=-0.5)) or similar does not work, but if I use margins as suggested here, e.g. theme(axis.text.x = element_text(margin = margin(t = -20))) all that changes is that the x axis title moves inwards into the plot area which expands, but the labels are still outside of the plot:

new plot using margins for x axis labels

How do I force ggplot to keep my labels inside the physical area of the plot?

dput of the data:

structure(list(Farm = c("Best", "Worst", "User"), CO2Fert = c(2.187635996, 
4.240789564, 1.189743632), CO2Manure = c(0.670289261, 0.503797793, 
2.852713125), CO2Feed = c(0.773605214, 1.884548328, 0.419821994
), CO2Housing = c(2.415801361, 5.114791701, 0.630429463), CO2Fuel = c(1.180859203, 
3.411847194, 2.982960729), CO2Other = c(2.876050311, 5.150085802, 
1.489036959), NO3Fert = c(2.19509301, 1.848317101, 1.643695528
), NO3Manure = c(2.452857906, 3.153028268, 1.922113286), NO3Grazing = c(0.037698451, 
4.452847769, 2.546101867), NH4Spreading = c(2.880954824, 2.60492997, 
3.186211336), NH4Storage = c(1.178815284, 4.893388111, 2.432823901
), NH4Grazing = c(0.509207305, 2.998872111, 4.444466334), NH4Housing = c(2.523406518, 
5.255666955, 1.287199958)), .Names = c("Farm", "CO2Fert", "CO2Manure", 
"CO2Feed", "CO2Housing", "CO2Fuel", "CO2Other", "NO3Fert", "NO3Manure", 
"NO3Grazing", "NH4Spreading", "NH4Storage", "NH4Grazing", "NH4Housing"
), row.names = c(NA, -3L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x0000000000120788>)
Hester Lyons
  • 803
  • 11
  • 24
  • provide data with `dput(dat2test)`. – Andre Elrico Mar 12 '18 at 13:43
  • [does this help?](https://stackoverflow.com/questions/17241182/how-to-make-geom-text-plot-within-the-canvass-bounds) – Andre Elrico Mar 12 '18 at 13:47
  • I have added my data at the bottom of the question. I'm looking at your link to see if it resolves the issue. Thank you. – Hester Lyons Mar 12 '18 at 13:50
  • I have tried `thePlot <- thePlot + theme(axis.text.x = element_text(vjust="inward", hjust="inward"))` but if I read your link correctly these settings for `vjust` only apply to `geom_text`. The above introduces an error (NAs introduced by coercion) but does not change the plot. – Hester Lyons Mar 12 '18 at 13:57

2 Answers2

0

one possible work around:

library(grid)

gt <- ggplot_gtable(ggplot_build(thePlot))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid::grid.draw(gt)

result:

enter image description here

Community
  • 1
  • 1
Andre Elrico
  • 10,956
  • 6
  • 50
  • 69
  • That is an interesting solution. At the moment, my plot building code is in a function that returns a ggplot object to a Shiny app, which then renders it in a plotOutput. I'm not sure that using this method I could return a similar object...? I'm going to see if I can make this work. Thank you. – Hester Lyons Mar 12 '18 at 14:11
  • Yes this works if I put the `grid::grid.draw(gt)` part in the renderPlot function as the final call, and keep the rest of the code in the function (i.e. pass `gt` back as the function result. Thank you for this. – Hester Lyons Mar 12 '18 at 14:16
  • glad to help bro. – Andre Elrico Mar 12 '18 at 14:24
0
# Simply add an annotation layer and remove y axis text

    thePlot <-  ggplot(data = dat2test, aes(x=variable, y=value, group=Farm, color= Farm)) + 
      geom_polygon(fill = NA) + 
      coord_polar()+
      annotate('text', x=.5, y=c(1,2,3,4,5), label=c(1,2,3,4,5))+
      theme(axis.text.y = element(blank),
            axis.tick.y = element(blank))