离散值供给了连续刻度错误,如果在循环中调用变量。

huangapple go评论103阅读模式
英文:

Discrete value supplied to continuous scale error if variables as called in a loop

问题

我想创建一个使用ggplot2的柱状图,每个变量都带有误差条,但我遇到了Error: Discrete value supplied to continuous scale的问题。我尝试使用以下代码:

# Packages
library(dplyr)
library(ggplot2)

# My data set:
ds.1.1 <- read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/exp_ivan_1.csv")
ds.1.2 <- ds.1.1 %>% dplyr::select(!EM)
ds.1.2.wide <- ds.1.2 |> 
  summarize(across(everything(), list(mean = ~mean(.x, na.rm = TRUE), sd = ~sd(.x, na.rm = TRUE)/sqrt(3))), .by = Tratamentos)
str(ds.1.2.wide)

# 'data.frame':	4 obs. of  17 variables:
#  $ Tratamentos: chr  "T-0" "T-2" "T-3" "T-5"
#  $ GE_mean    : num  0.78 0.695 0.71 0.685
#  $ GE_sd      : num  0.00667 0.02887 0.01633 0.01106
#  $ PC_mean    : num  0.56 0.49 0.455 0.435
#  $ PC_sd      : num  0.00943 0.00667 0.0318 0.01732
#  $ IVE_mean   : num  5.61 4.92 4.88 4.78
#  $ IVE_sd     : num  0.0893 0.2382 0.2879 0.3859
#  $ CE_mean    : num  50.5 59.1 53.7 62.1
#  $ CE_sd      : num  0.355 0.993 0.251 0.721
#  $ AM_mean    : num  0.707 0.583 0.601 0.604
#  $ AM_sd      : num  0.01371 0.00947 0.0226 0.02371
#  $ EE_mean    : num  0.0201 0.0249 0.0233 0.0216
#  $ EE_sd      : num  0.000347 0.000416 0.000347 0.001319
#  $ FB_mean    : num  0.291 0.283 0.296 0.307
#  $ FB_sd      : num  0.002886 0.005818 0.000816 0.005005
#  $ PB_mean    : num  0.105 0.492 0.114 0.111
#  $ PB_sd      : num  0.00042 0.31233 0.00333 0.00565

plots_pull <- list()
target.columns <- c(2, 4, 6, 8, 10, 12, 14, 16)
for (j in 1:length(target.columns)) {
  ds.1.2.wide.sub <- ds.1.2.wide |>
    dplyr::select(Tratamentos, colnames(ds.1.2.wide)[target.columns[j]], colnames(ds.1.2.wide)[target.columns[j] + 1])
  
  # The errorbars overlapped, so use position_dodge to move them horizontally
  pd <- position_dodge(0.1)
  
  # Use 95% confidence interval instead of SEM
  plots_pull[[j]] <- ggplot(data = ds.1.2.wide.sub, aes(x = Tratamentos, y = .data[[colnames(ds.1.2.wide.sub)[2]]])) +
    geom_errorbar(mapping = aes(ymin = .data[[colnames(ds.1.2.wide.sub)[2]]] - .data[[colnames(ds.1.2.wide.sub)[3]]],
                                ymax = .data[[colnames(ds.1.2.wide.sub)[2]]] + .data[[colnames(ds.1.2.wide.sub)[3]]]),
                  position = pd, width = 0.2) +
    geom_line(aes(linetype = Tratamentos), position = pd) +
    scale_colour_grey(start = 0, end = .5) +
    theme_bw() + theme(
      panel.border = element_blank(), panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
      axis.text.x = element_text(angle = 0, hjust = 0),
      axis.text.y = element_text(angle = 0, hjust = 0),
      text = element_text(size = 18, family = "serif"), legend.position = "none"
    )
}
plots_pull

请注意,我已经更正了您代码中的一些HTML实体编码,以便在R中正确解析。希望这可以解决您遇到的问题。

英文:

I'd like to create a ggplot2 barplot with error bars for each variable, but I'm having trouble with Error: Discrete value supplied to continuous scale. I've tried to use the following code:

# Packages
library(dplyr)
library(ggplot2)
# My data set:
ds.1.1 &lt;- read.csv(&quot;https://raw.githubusercontent.com/Leprechault/trash/main/exp_ivan_1.csv&quot;)
ds.1.2 &lt;- ds.1.1%&gt;%dplyr::select(!EM)
ds.1.2.wide &lt;- ds.1.2|&gt; summarize(across(everything(), list(mean = ~mean(.x, na.rm = TRUE), sd = ~sd(.x, na.rm = TRUE)/sqrt(3))), .by = Tratamentos)
str(ds.1.2.wide)
# &#39;data.frame&#39;:	4 obs. of  17 variables:
#  $ Tratamentos: chr  &quot;T-0&quot; &quot;T-2&quot; &quot;T-3&quot; &quot;T-5&quot;
#  $ GE_mean    : num  0.78 0.695 0.71 0.685
#  $ GE_sd      : num  0.00667 0.02887 0.01633 0.01106
#  $ PC_mean    : num  0.56 0.49 0.455 0.435
#  $ PC_sd      : num  0.00943 0.00667 0.0318 0.01732
#  $ IVE_mean   : num  5.61 4.92 4.88 4.78
#  $ IVE_sd     : num  0.0893 0.2382 0.2879 0.3859
#  $ CE_mean    : num  50.5 59.1 53.7 62.1
#  $ CE_sd      : num  0.355 0.993 0.251 0.721
#  $ AM_mean    : num  0.707 0.583 0.601 0.604
#  $ AM_sd      : num  0.01371 0.00947 0.0226 0.02371
#  $ EE_mean    : num  0.0201 0.0249 0.0233 0.0216
#  $ EE_sd      : num  0.000347 0.000416 0.000347 0.001319
#  $ FB_mean    : num  0.291 0.283 0.296 0.307
#  $ FB_sd      : num  0.002886 0.005818 0.000816 0.005005
#  $ PB_mean    : num  0.105 0.492 0.114 0.111
#  $ PB_sd      : num  0.00042 0.31233 0.00333 0.00565
plots_pull &lt;- list()
target.columns &lt;- c(2,4,6,8,10,12,14,16)
for (j in 1:length(target.columns)){
ds.1.2.wide.sub&lt;-ds.1.2.wide|&gt;dplyr::select(Tratamentos,colnames(ds.1.2.wide)[target.columns[j]],colnames(ds.1.2.wide)[target.columns[j]+1])   
# The errorbars overlapped, so use position_dodge to move them horizontally
pd &lt;- position_dodge(0.1)#position_jitter(height = 0.001) # move them .05 to the left and right
# Use 95% confidence interval instead of SEM
plots_pull[[j]] &lt;-ggplot(data=ds.1.2.wide.sub, aes(x=Tratamentos, y=colnames(ds.1.2.wide.sub)[2])) +
geom_errorbar(data=ds.1.2.wide.sub,mapping=aes(ymin=ds.1.2.wide.sub[2]-ds.1.2.wide.sub[3], ymax=ds.1.2.wide.sub[2]-ds.1.2.wide.sub[3]),position=pd, width=0.2) +
geom_line(aes(linetype = Tratamentos), position = pd) +
scale_colour_grey(start = 0, end = .5) +
theme_bw()  + theme(
panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = &quot;black&quot;),
axis.text.x=element_text(angle = 0, hjust = 0),
axis.text.y=element_text(angle = 0, hjust = 0),
text=element_text(size=18,  family=&quot;serif&quot;),legend.position=&quot;none&quot;)
}
plots_pull    
#[[1]]
#Don&#39;t know how to automatically pick scale for object of type ##&lt;data.frame&gt;.
#Defaulting to continuous.
#Error: Discrete value supplied to continuous scale

I maked a partial inspection and the error means to be in:

pd &lt;- position_dodge(0.1)#position_jitter(height = 0.001) # move them .05 to the left and right
ggplot(data=ds.1.2.wide.sub, aes(x=Tratamentos, y=colnames(ds.1.2.wide.sub)[2])) +
geom_errorbar(data=ds.1.2.wide.sub,mapping=aes(ymin=ds.1.2.wide.sub[2]-ds.1.2.wide.sub[3], ymax=ds.1.2.wide.sub[2]-ds.1.2.wide.sub[3]),position=pd, width=0.2) +
geom_line(aes(linetype = Tratamentos), position = pd) +
scale_colour_grey(start = 0, end = .5) +
theme_bw() 
# Don&#39;t know how to automatically pick scale for object of type &lt;data.frame&gt;. Defaulting to continuous.
# ERROR while rich displaying an object: Error: Discrete value supplied to continuous scale

Please, any help will be welcome.

答案1

得分: 1

这是已经翻译好的部分:

"NOTE: This answer doesn't solve your question, but I believe it solves the underlying problem that leads to your question. I hope it's useful!"

"I tried reproducing your plot, and it seems the problem lies in how messy your data is. The main problem is that each column of your dataframe says two things about their values: both if they are mean or sd, and their category (GE, PC, etc). This makes it difficult to pinpoint the value we need for our plots, causing issues further: you had to solve this through an intricate system to select which columns you wanted for each plot."

"Let's clean up the data first:"

"library(dplyr)"
"library(stringr)"
"library(tidyr)"

"ds_2 <- ds.1.2.wide |>"
"as_tibble() |>"
"#we first convert to long, so that we have one row per data point"
"tidyr::pivot_longer(2:17) |>"
"#we extract the data that describes each data point from the column name into its own columns"
"mutate("
"category = stringr::str_extract(name, ".") |> stringr::str_remove(""),"
"type = stringr::str_extract(name, "_.
") |> stringr::str_remove("_")) |>"
"select(-name) |>"
"#we no longer need the name column, as now we have two columns that separate it"
"#finally, we transform our data agan so that each combination of treatment and category is in its own row, and the data points asociated with them are in their own columns"
"tidyr::pivot_wider(names_from = "type", values_from = "value")"

"Now, we can plot each category of data simply by filtering the "category" column:"

"ds_2 |>"
"filter(category == "GE") |>"
"ggplot(aes(Tratamentos, mean)) +"
"geom_line(position = position_dodge(0.1)) +"
"geom_errorbar(mapping = aes(ymin=mean-sd, ymax=mean+sd), width=0.2) +"
"scale_colour_grey(start = 0, end = .5) +"
"theme_bw() +"
"theme("
"panel.border = element_blank(), panel.grid.major = element_blank(),"
"panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),"
"axis.text.x=element_text(angle = 0, hjust = 0),"
"axis.text.y=element_text(angle = 0, hjust = 0),"
"text=element_text(size=18, family="serif"),legend.position="none")"

"Therefore, the loop becomes way simpler:"

"categories <- unique(ds_2$category)"

"plots_new <- list()"

"for (j in categories){"

"plots_new[[j]] <- ds_2 |>"
"#filter data"
"filter(category == j) |>"
"#plot"
"ggplot(aes(Tratamentos, mean)) +"
"geom_line(position = position_dodge(0.1)) +"
"geom_errorbar(mapping = aes(ymin=mean-sd, ymax=mean+sd), width=0.2) +"
"scale_colour_grey(start = 0, end = .5) +"
"theme_bw() +"
"theme("
"panel.border = element_blank(), panel.grid.major = element_blank(),"
"panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),"
"axis.text.x=element_text(angle = 0, hjust = 0),"
"axis.text.y=element_text(angle = 0, hjust = 0),"
"text=element_text(size=18, family="serif"),legend.position="none") +"
"labs(subtitle = j)"
"}"

"plots_new[[6]]"

英文:

NOTE: This answer doesn't solve your question, but I believe it solves the underlying problem that leads to your question. I hope it's useful!

I tried reproducing your plot, and it seems the problem lies in how messy your data is. The main problem is that each column of your dataframe says two things about their values: both if they are mean or sd, and their category (GE, PC, etc). This makes it difficult to pinpoint the value we need for our plots, causing issues further: you had to solve this through an intricate system to select which columns you wanted for each plot.

Let's clean up the data first:

library(dplyr)
library(stringr)
library(tidyr)
ds_2 &lt;- ds.1.2.wide |&gt; 
as_tibble() |&gt; 
#we first convert to long, so that we have one row per data point
tidyr::pivot_longer(2:17) |&gt;
#we extract the data that describes each data point from the column name into its own columns
mutate(
category = stringr::str_extract(name, &quot;.*_&quot;) |&gt; stringr::str_remove(&quot;_&quot;), 
type = stringr::str_extract(name, &quot;_.*&quot;) |&gt; stringr::str_remove(&quot;_&quot;)) |&gt; 
select(-name) |&gt; #we no longer need the name column, as now we have two columns that separate it
#finally, we transform our data agan so that each combination of treatment and category is in its own row, and the data points asociated with them are in their own columns
tidyr::pivot_wider(names_from = &quot;type&quot;, values_from = &quot;value&quot;)

Now, we can plot each category of data simply by filtering the "category" column:

ds_2 |&gt; 
filter(category == &quot;GE&quot;) |&gt; 
ggplot(aes(Tratamentos, mean)) +
geom_line(position = position_dodge(0.1)) +
geom_errorbar(mapping = aes(ymin=mean-sd, 
ymax=mean+sd),
width=0.2) +
scale_colour_grey(start = 0, end = .5) +
theme_bw() +
theme(
panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = &quot;black&quot;),
axis.text.x=element_text(angle = 0, hjust = 0),
axis.text.y=element_text(angle = 0, hjust = 0),
text=element_text(size=18,  family=&quot;serif&quot;),legend.position=&quot;none&quot;)

Therefore, the loop becomes way simpler:

categories &lt;- unique(ds_2$category)
plots_new &lt;- list()
for (j in categories){
plots_new[[j]] &lt;- ds_2 |&gt; 
#filter data
filter(category == j) |&gt; 
#plot
ggplot(aes(Tratamentos, mean)) +
geom_line(position = position_dodge(0.1)) +
geom_errorbar(mapping = aes(ymin=mean-sd, 
ymax=mean+sd),
width=0.2) +
scale_colour_grey(start = 0, end = .5) +
theme_bw() +
theme(
panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = &quot;black&quot;),
axis.text.x=element_text(angle = 0, hjust = 0),
axis.text.y=element_text(angle = 0, hjust = 0),
text=element_text(size=18,  family=&quot;serif&quot;),legend.position=&quot;none&quot;) +
labs(subtitle = j)
}
plots_new[[6]]

答案2

得分: 1

主要问题是,使用y=colnames(ds.1.2.wide.sub)[2],你正在将字符字符串(即离散变量)映射到y美学。如果将列名作为字符字符串传递,那么你必须使用.data代词或!!sym()告诉ggplot2该字符字符串是数据中的列名。

此外,你可以通过使用purrr::map2来简化你的代码,而不是使用for循环,通过将绘图代码放入函数中,并直接循环遍历列名而不是位置:

library(ggplot2)
library(dplyr)

target.columns <- c(2, 4, 6, 8, 10, 12, 14, 16)

# 直接循环遍历列名
mean_cols <- names(ds.1.2.wide)[target.columns]
sd_cols <- names(ds.1.2.wide)[target.columns + 1]

plot_fun <- function(x, y) {
  ggplot(
    data = ds.1.2.wide,
    aes(
      x = Tratamentos,
      y = .data[[x]]
    )
  ) +
    geom_errorbar(
      aes(
        ymin = .data[[x]] - .data[[y]],
        ymax = .data[[x]] + .data[[y]]
      ),
      width = 0.2
    ) +
    geom_line(aes(group = 1)) +
    scale_colour_grey(start = 0, end = 0.5) +
    theme_bw() +
    theme(
      panel.border = element_blank(),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      axis.line = element_line(colour = "black"),
      axis.text.x = element_text(angle = 0, hjust = 0),
      axis.text.y = element_text(angle = 0, hjust = 0),
      text = element_text(size = 18, family = "serif"),
      legend.position = "none"
    )
}

plots_pull <- purrr::map2(mean_cols, sd_cols, plot_fun)

# 仅供演示
library(patchwork)

plots_pull |>
  wrap_plots()

离散值供给了连续刻度错误,如果在循环中调用变量。

英文:

The main issue is that using y=colnames(ds.1.2.wide.sub)[2] you are mapping a character string aka a discrete variable on the y aesthetic. If you pass a column name as a character string then you have to use the .data pronoun or !!sym() to tell ggplot2 that the character string is the name of a column in your data.

Besides that you can simplify your code considerably by switching to e.g. purrr::map2 instead of using a for loop, by putting your plotting code in a function and by looping directly over column names instead of positions:

library(ggplot2)
library(dplyr)

target.columns &lt;- c(2, 4, 6, 8, 10, 12, 14, 16)

# Loop directly over the column names
mean_cols &lt;- names(ds.1.2.wide)[target.columns]
sd_cols &lt;- names(ds.1.2.wide)[target.columns + 1]

plot_fun &lt;- function(x, y) {
  ggplot(
    data = ds.1.2.wide,
    aes(
      x = Tratamentos,
      y = .data[[x]]
    )
  ) +
    geom_errorbar(
      aes(
        ymin = .data[[x]] - .data[[y]],
        ymax = .data[[x]] + .data[[y]]
      ),
      width = 0.2
    ) +
    geom_line(aes(group = 1)) +
    scale_colour_grey(start = 0, end = .5) +
    theme_bw() +
    theme(
      panel.border = element_blank(),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      axis.line = element_line(colour = &quot;black&quot;),
      axis.text.x = element_text(angle = 0, hjust = 0),
      axis.text.y = element_text(angle = 0, hjust = 0),
      text = element_text(size = 18, family = &quot;serif&quot;),
      legend.position = &quot;none&quot;
    )
}

plots_pull &lt;- purrr::map2(mean_cols, sd_cols, plot_fun)

# Just for illustration purposes
library(patchwork)

plots_pull |&gt; 
  wrap_plots()

离散值供给了连续刻度错误,如果在循环中调用变量。<!-- -->

答案3

得分: 1

以下是您的代码部分的翻译:

# 需要用于绘制所有“列对”在同一图中吗?你有位置分开的设置,但你也尝试绘制单独的图形。
# 使用geom_line的目的不清楚。我猜你是想连接从T0到T5的误差条吗?还是只想得到均值?

# 通常,您用于ggplot的输入数据表/数据框不够干净,或者不太适合ggplot处理。以下是一些建议:

# 首先,(特别是如果您想在一个图中获取所有内容),最好将数据框转换为每行一个观察值(长格式)。

df <- ds.1.2.wide %>%
  gather(test, value, -Tratamentos) %>%
  separate(test, into = c("test", "stat")) %>%
  spread(stat, value)

# 接下来,虽然使用for循环可能不是一个好主意,但这是最小程度地修正您的原始代码的方法(我也试图纠正了缩进):

target.tests <- unique(df$test)
plots_pull <- list()
for (j in 1:length(target.tests)){
  # 使用95%置信区间而不是SEM # 注意+- SD与CI不同...
  plots_pull[[j]] <- df %>%
    filter(test == !!target.tests[j]) %>%
    ggplot() +
    geom_errorbar(mapping = aes(x = Tratamentos, ymin = mean - sd, ymax = mean + sd), width = 0.2) +
    geom_line(aes(x = Tratamentos, y = mean, linetype = test, group = test)) +
    scale_colour_grey(start = 0, end = .5) + # 不确定为什么需要这个
    theme_bw()  + 
    theme(panel.border = element_blank(),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          axis.line = element_line(colour = "black"),
          axis.text.x=element_text(angle = 0, hjust = 0),
          axis.text.y=element_text(angle = 0, hjust = 0),
          text=element_text(size=18,  family="serif"),
          legend.position="none")
}

plots_pull

# 如果您的意思是将所有内容绘制到一个图中(然后您需要闪避):

pd <- position_dodge(0.1)
df %>%
  ggplot() +
  geom_errorbar(mapping = aes(x = Tratamentos, ymin = mean - sd, ymax = mean + sd, group = test), position=pd, width = 0.2) +
  geom_line(aes(x = Tratamentos, y = mean, linetype = test, group = test), position=pd) +
  scale_colour_grey(start = 0, end = .5) + # 不确定为什么需要这个
  theme_bw()  + 
  theme(panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_line(colour = "black"),
        axis.text.x=element_text(angle = 0, hjust = 0),
        axis.text.y=element_text(angle = 0, hjust = 0),
        text=element_text(size=18,  family="serif"),
        legend.position="none")

# 如果您的意思是绘制均值而不是连接误差条,则应使用另一个geom_errorbar而不是geom_line:

pd <- position_dodge(0.1)
df %>%
  ggplot() +
  geom_errorbar(mapping = aes(x = Tratamentos, ymin = mean - sd, ymax = mean + sd, group = test), position=pd, width = 0.2) +
  geom_errorbar(aes(x = Tratamentos, ymin = mean, ymax = mean, group = test), position=pd, width = 0.3) + # 那么不需要linetype了?
  scale_colour_grey(start = 0, end = .5) + # 不确定为什么需要这个
  theme_bw()  + 
  theme(panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_line(colour = "black"),
        axis.text.x=element_text(angle = 0, hjust = 0),
        axis.text.y=element_text(angle = 0, hjust = 0),
        text=element_text(size=18,  family="serif"),
        legend.position="none")

# 如果您坚持使用您的数据框结构,那么您需要像这样添加!!rlang::sym()到所有列名,以供ggplot使用,否则ggplot将认为列名是字符串而不是变量:

ggplot() +
    geom_errorbar(data=ds.1.2.wide.sub,
                  mapping=aes(x=Tratamentos, 
                              ymin=!!rlang::sym(colnames(ds.1.2.wide.sub)[2]) - !!rlang::sym(colnames(ds.1.2.wide.sub)[3]),
                              ymax=!!rlang::sym(colnames(ds.1.2.wide.sub)[2]) + !!rlang::sym(colnames(ds.1.2.wide.sub)[3])),
                  width=0.2) +
    theme_bw()

请注意,这是您的代码的翻译,不包括问题的答案或其他内容。

英文:

Multiple things that are confusing in your code:

  1. Are you trying to plot all the "column pairs" in the same graph? You have the position dodge for that but you also try to plot a separate graph.
  2. It is unclear what you want to achieve by the geom_line. I guess you are trying to connect the error bars from T0 to T5? Or do you just want to get the mean?

Generally, your input tibble/data frame for ggplot is not clean enough or in a structure that is not ideal for handling by ggplot. Here are some suggestions:

Firstly, (especially if you want to get everything in one graph), it is best to turn the data frame into one observation per line (long format).

df &lt;- ds.1.2.wide %&gt;% 
gather(test, value, -Tratamentos) %&gt;% 
separate(test, into = c(&quot;test&quot;, &quot;stat&quot;)) %&gt;% 
spread(stat, value)

Next, although it may not be advisable to use a for loop, here is the way to minimally correct your original code (I also try to correct for the indents):

target.tests &lt;- unique(df$test)
plots_pull &lt;- list()
for (j in 1:length(target.tests)){
# Use 95% confidence interval instead of SEM # note that +- SD is not the same as CI...
plots_pull[[j]] &lt;- df %&gt;% 
filter(test == !!target.tests[j]) %&gt;%
ggplot() +
geom_errorbar(mapping = aes(x = Tratamentos, ymin = mean - sd, ymax = mean + sd), width = 0.2) +
geom_line(aes(x = Tratamentos, y = mean, linetype = test, group = test)) +
scale_colour_grey(start = 0, end = .5) + # not sure why you need this
theme_bw()  + 
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = &quot;black&quot;),
axis.text.x=element_text(angle = 0, hjust = 0),
axis.text.y=element_text(angle = 0, hjust = 0),
text=element_text(size=18,  family=&quot;serif&quot;),
legend.position=&quot;none&quot;)
}
plots_pull

In case, you mean to plot everything into one graph (then you need to dodge):

pd &lt;- position_dodge(0.1)
# target.tests &lt;- unique(df$test) # in this case you do not need this and no loop is needed
df %&gt;% 
ggplot() +
geom_errorbar(mapping = aes(x = Tratamentos, ymin = mean - sd, ymax = mean + sd, group = test), position=pd, width = 0.2) +
geom_line(aes(x = Tratamentos, y = mean, linetype = test, group = test), position=pd) +
scale_colour_grey(start = 0, end = .5) + # not sure why you need this
theme_bw()  + 
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = &quot;black&quot;),
axis.text.x=element_text(angle = 0, hjust = 0),
axis.text.y=element_text(angle = 0, hjust = 0),
text=element_text(size=18,  family=&quot;serif&quot;),
legend.position=&quot;none&quot;)

In case you meant to plot the mean instead of jointing the error bars, then you should use another geom_errorbar but not geom_line:

pd &lt;- position_dodge(0.1)
df %&gt;% 
ggplot() +
geom_errorbar(mapping = aes(x = Tratamentos, ymin = mean - sd, ymax = mean + sd, group = test), position=pd, width = 0.2) +
geom_errorbar(aes(x = Tratamentos, ymin = mean, ymax = mean, group = test), position=pd, width = 0.3) + # then not linetype is needed?
scale_colour_grey(start = 0, end = .5) + # not sure why you need this
theme_bw()  + 
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = &quot;black&quot;),
axis.text.x=element_text(angle = 0, hjust = 0),
axis.text.y=element_text(angle = 0, hjust = 0),
text=element_text(size=18,  family=&quot;serif&quot;),
legend.position=&quot;none&quot;)

If you insist on using your data frame structure, then you will need something like this (adding !!rlang::sym() to all the column names for ggplot, otherwise ggplot will think the column name as a string rather than a variable):

ggplot() +
geom_errorbar(data=ds.1.2.wide.sub,
mapping=aes(x=Tratamentos, 
ymin=!!rlang::sym(colnames(ds.1.2.wide.sub)[2]) - !!rlang::sym(colnames(ds.1.2.wide.sub)[3]),
ymax=!!rlang::sym(colnames(ds.1.2.wide.sub)[2]) + !!rlang::sym(colnames(ds.1.2.wide.sub)[3])),
width=0.2) +
theme_bw() 

huangapple
  • 本文由 发表于 2023年8月11日 00:20:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定