1

我正在尝试制作一个小提琴图,显示不同观察日期的昆虫访问植物品种的次数。我有 25 种不同的植物类型和 16 个不同的连续日期。

尽管(尝试)正确格式化日期,但我不断遇到错误,例如:“as.Date.numeric(value) 中的错误:必须提供'origin'”。这是我的代码:

violin_date<-df, sep = ",", header = TRUE, row.names(1))

violin_date$Date
violin_date$Date <- as.Date.character(violin_date$Date, format = 
"%m/%d/%y", origin = 1)

violin_date[, 2:26] <- log(violin_date[2:26])
is.na(violin_date) <- do.call(cbind,lapply(violin_date, is.infinite))
violin_date[is.na(violin_date)] <- 0

violin_date_melt = melt(as.matrix(violin_date))

ggplot (violin_date) + labs(x="Cultivar",y="Date") + geom_violin(aes(x 
= variable, y = Date), trim = FALSE) + coord_flip() 

我的数据标题如下所示:

    Date      MTO       MY      MDO       MR       MF      LSR       LY        LC
1 2016-07-06 3.895948 2.379877 3.496892 3.825758 2.137259 0.000000 3.829630 4.467857
2 2016-07-07 0.000000 4.448635 2.955516 0.000000 3.275105 0.000000 0.000000 3.592755
3 2016-07-13 6.685971 5.180261 5.459738 6.297448 6.073368 8.222671 7.026091 0.000000
4 2016-07-21 5.327740 6.049159 6.408181 4.745223 6.451136 0.000000 0.000000 0.000000
5 2016-07-25 5.671947 5.102823 4.998650 4.549558 5.604289 0.000000 0.000000 0.000000
6 2016-07-26 5.685996 4.632958 6.095359 5.348789 5.006001 0.000000 0.000000 4.806940

任何帮助表示赞赏!

4

1 回答 1

0

您可以尝试几件事。

  1. 使用readr导入数据集。从您提供的数据集的外观来看,readr::read_delim将自动以正确的存储模式读取日期。

  2. 使用dplyrtidyr修改数据集,以便您可以构建想要的小提琴图。

听到是一个插图:

library(readr)
library(dplyr)
library(tidyr)
library(ggplot2)


# Local data example
data_file <-
"Date MTO MY MDO MR MF LSR LY LC
2016-07-06 3.895948 2.379877 3.496892 3.825758 2.137259 0.000000 3.829630 4.467857
2016-07-07 0.000000 4.448635 2.955516 0.000000 3.275105 0.000000 0.000000 3.592755
2016-07-13 6.685971 5.180261 5.459738 6.297448 6.073368 8.222671 7.026091 0.000000
2016-07-21 5.327740 6.049159 6.408181 4.745223 6.451136 0.000000 0.000000 0.000000
2016-07-25 5.671947 5.102823 4.998650 4.549558 5.604289 0.000000 0.000000 0.000000
2016-07-26 5.685996 4.632958 6.095359 5.348789 5.006001 0.000000 0.000000 4.806940
"

# Use readr::read_delim to read in the data set.  You can explicity define the
# colume types via col_types, but the data is well formated and the default
# guesses will work well.  Instead of the `data_file` used in this example,
# you'll want to use the path to the actual data file.
violin_data <- read_delim(data_file, delim = " ")
str(violin_data, max.level = 1)

# Use tidyr::gather instead of reshape2::melt
violin_data <- gather(violin_data, key = variable, value = value, -Date)
str(violin_data)

# create a weight variable for each variable.  dplyr will make this easy.
violin_data <-
  violin_data %>%
  group_by(variable) %>%
  mutate(wt = value / sum(value)) %>%
  ungroup()

# build the plot
ggplot(violin_data) +
  aes(x = variable, y = Date, weight = wt) +
  geom_violin()

在此处输入图像描述

于 2017-04-30T16:29:54.677 回答