我有以下数据集。请注意,这些都是虚构的值:
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
我被要求绘制这些值的蜘蛛图。看了fmsb和ggradar之后,我有很多理由决定从头开始。所以,我有以下代码(目前还不确定是否所有库都是必需的):
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 main text I've taken my code from),这似乎无法在任何地方解决。这个here有一个有趣的解决方案,我正在考虑使用它,但我的最终标签可能很长且具有描述性。我希望它们能够水平显示,因为它们现在有适当的换行符,但它们适合图表。
到目前为止,我对此并不是很了解。使用theme(axis.title.x = element_text(vjust=-0.5))
或类似功能不起作用,但如果我使用建议here的边距,例如theme(axis.text.x = element_text(margin = margin(t = -20)))
所有改变的是x轴标题向内移动到扩展的绘图区域,但标签仍在图表之外:
如何强制ggplot将我的标签保留在绘图的物理区域内?
数据输入:
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>)
答案 0 :(得分:0)
一种可能的解决方法:
library(grid)
gt <- ggplot_gtable(ggplot_build(thePlot))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid::grid.draw(gt)
答案 1 :(得分: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))