【发布时间】:2018-03-12 13:22:48
【问题描述】:
我有以下数据集。请注意,这些是虚构的值:
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>)
【问题讨论】:
-
用
dput(dat2test)提供数据。 -
我已在问题底部添加了我的数据。我正在查看您的链接,看看它是否可以解决问题。谢谢。
-
我尝试过
thePlot <- thePlot + theme(axis.text.x = element_text(vjust="inward", hjust="inward")),但如果我正确阅读了您的链接,vjust的这些设置仅适用于geom_text。上面引入了一个错误(强制引入的NAs),但不会改变情节。