在 R 中生成不同颜色的刚性图

问题描述 投票:0回答:1

我正在尝试使用 USGS 开发的 R 包生成一些僵硬的图表:https://pubs.usgs.gov/publication/ofr20161188。这些数字生成得很好,但我在用不同的颜色对各个僵硬的图进行着色时遇到了问题。您将在下面的示例代码中看到,我试图将它们分为两个颜色组,但该图正在为所有四个刚性图应用单一颜色。任何围绕此问题的解决方案将不胜感激。预先感谢。

# Load required packages
require(smwrGraphs)
require(smwrData)

# prepare stiff diagram space
setSweave("stiffplot", 6, 6)
AA.lo <- setLayout(height=3.5, explanation=list(bottom=1.1))
setGraph(1, AA.lo)

# get the environmental data
data(MiscGW)

# convert to appropriate concentrations
PD <- transform(MiscGW, Ca.meq = conc2meq(Calcium, "calcium"),
                Mg.meq = conc2meq(Magnesium, "magnesium"),
                Na.meq = conc2meq(Sodium, "sodium"),
                Cl.meq = conc2meq(Chloride, "chloride"),
                SO4.meq = conc2meq(Sulfate, "sulfate"),
                HCO3.meq = conc2meq(Bicarbonate, "bicarb"))

PD$SS <- row.names(PD)

# Generate the plot
AA.pl <- with(PD, stiffPlot(cbind(Ca.meq, Mg.meq, Na.meq)
                            , cbind(Cl.meq, SO4.meq, HCO3.meq), ylabels=SS
                            , caption = "Stiff diagrams"
                            , Stiff= list(fill = c("#228B22", "#228B22", "#00688B", "#00688B"))
                            )
              )
setGraph("explanation", AA.lo)
addExplanation(AA.pl)

graphics.off()
r
1个回答
0
投票

这可以被认为是作弊,但我发现的唯一方法是使用

par
函数并在彼此下方创建单独的图。主要问题是,Stiffplot 仅采用
Stiff
中的一种颜色以及应用于所有多边形的颜色。因此
Stiff= list(fill = c("#228B22", "#228B22", "#00688B", "#00688B"))
不起作用!

#install.packages("remotes")
#remotes::install_gitlab("water/analysis-tools/smwrData", host = "code.usgs.gov")
#remotes::install_gitlab("water/analysis-tools/smwrGraphs", host = "code.usgs.gov")

library(smwrData)
library(smwrGraphs)
library(dataRetrieval)

# Prepare stiff diagram space
setSweave("stiffplot", 6, 6)
AA.lo <- setLayout(height=3.5, explanation=list(bottom=1.1))
setGraph(1, AA.lo)

# Get the environmental data
data(MiscGW)

# Check if necessary columns are available in the data
required_cols <- c("Calcium", "Magnesium", "Sodium", "Chloride", "Sulfate", "Bicarbonate")
if (!all(required_cols %in% colnames(MiscGW))) {
  stop("Missing one or more required columns in the MiscGW dataset")
}

# Convert to appropriate concentrations
PD <- transform(MiscGW, 
                Ca.meq = conc2meq(Calcium, "calcium"),
                Mg.meq = conc2meq(Magnesium, "magnesium"),
                Na.meq = conc2meq(Sodium, "sodium"),
                Cl.meq = conc2meq(Chloride, "chloride"),
                SO4.meq = conc2meq(Sulfate, "sulfate"),
                HCO3.meq = conc2meq(Bicarbonate, "bicarb"))

PD$SS <- row.names(PD)



# Set up the plotting layout
par(mfrow=c(6,1))  # Adjust margins for all plots

# First Stiff diagram
stiffPlot(
  cbind(PD$Ca.meq, PD$Mg.meq, PD$Na.meq)[1,],
  cbind(PD$Cl.meq, PD$SO4.meq, PD$HCO3.meq)[1,],
  ylabels = PD$SS[1],
  Stiff = list(fill = "#228B22")
)

# Second Stiff diagram
stiffPlot(
  cbind(PD$Ca.meq, PD$Mg.meq, PD$Na.meq)[2,],
  cbind(PD$Cl.meq, PD$SO4.meq, PD$HCO3.meq)[2,],
  ylabels = PD$SS[2],
  Stiff = list(fill = "#228B22")
)

# Third Stiff diagram
stiffPlot(
  cbind(PD$Ca.meq, PD$Mg.meq, PD$Na.meq)[3,],
  cbind(PD$Cl.meq, PD$SO4.meq, PD$HCO3.meq)[3,],
  ylabels = PD$SS[3],
  Stiff = list(fill = "#00688B")
)

# Fourth Stiff diagram with extra top margin
stiffPlot(
  cbind(PD$Ca.meq, PD$Mg.meq, PD$Na.meq)[4,],
  cbind(PD$Cl.meq, PD$SO4.meq, PD$HCO3.meq)[4,],
  ylabels = PD$SS[4],
  caption = "Stiff diagrams",
  Stiff = list(fill = "#00688B")
)

setGraph("explanation", AA.lo)
addExplanation(AA.pl)


# Close the graphics device
graphics.off()

它看起来像这样: out

© www.soinside.com 2019 - 2024. All rights reserved.