简体   繁体   English

在蜘蛛图中移动ggplot x轴标签文本

[英]Moving ggplot x axis label text in a spider diagram

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. 看过fmsb和ggradar之后,出于多种原因,我决定从头开始。 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: 该图表如下所示: 简单蜘蛛图的图像

As you can see the labels are not fully onto the plot surface. 如您所见,标签未完全贴在图面上。 Looking at the examples around on the internet (eg 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 , eg 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: 使用theme(axis.title.x = element_text(vjust=-0.5))或类似方法不起作用,但是如果我按照此处的建议使用边距,例如theme(axis.text.x = element_text(margin = margin(t = -20)))所有变化是x轴标题向内移动到扩展的绘图区域中,但标签仍在绘图外部:

使用x轴标签边距的新图

How do I force ggplot to keep my labels inside the physical area of the plot? 如何强制ggplot将标签保留在绘图的物理区域内?

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>)

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: 结果:

在此处输入图片说明

# 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM