You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否将df_poisson的17个变量绘制为同一输出中的Poisson折线图?

当然可以绘制17条折线(含Poisson拟合折线)!

首先,你的原始数据存在一个小问题:代码里重复定义了V11,我先把它修正为V12,确保数据框结构正确。接下来分两种场景实现你的需求:


场景1:直接绘制每个变量的观测值折线

如果你的需求是把每个变量的6个观测值连成折线,同一图展示17条线,需要先把宽格式数据转成长格式(ggplot2更适合处理长格式),然后绘图:

步骤1:修正并准备数据

# 修正原始数据框的重复V11问题
df_poisson <- structure(list(
  V1 = c(131L, 38L, 19L, 96L, 13L, 34L), 
  V2 = c(1L, 1L, 1L, 1L, 0L, 1L), 
  V3 = c(104L, 30L, 20L, 72L, 21L, 62L), 
  V4 = c(20L, 12L, 7L, 14L, 7L, 18L), 
  V5 = c(38L, 9L, 12L, 31L, 10L, 18L), 
  V6 = c(2L, 0L, 1L, 2L, 0L, 7L), 
  V7 = c(293L, 87L, 44L, 217L, 46L, 156L), 
  V8 = c(1L, 0L, 0L, 0L, 0L, 0L), 
  V9 = c(0L, 1L, 0L, 0L, 0L, 0L), 
  V10 = c(1L, 1L, 1L, 0L, 0L, 1L), 
  V11 = c(1L, 1L, 1L, 1L, 1L, 1L), 
  V12 = c(1L, 1L, 1L, 1L, 0L, 1L),  # 修正重复的V11为V12
  V13 = c(4L, 1L, 1L, 0L, 0L, 2L), 
  V14 = c(0L, 0L, 0L, 0L, 0L, 0L), 
  V15 = c(0L, 0L, 0L, 0L, 0L, 0L), 
  V16 = c(0L, 0L, 0L, 0L, 0L, 0L), 
  V17 = c(0L, 0L, 0L, 0L, 0L, 0L)
), .Names = c("V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", "V12", "V13", "V14", "V15", "V16", "V17"), row.names = c(NA, 6L), class = "data.frame")

# 加载需要的包
library(tidyr)
library(ggplot2)

# 转长格式,添加观测索引作为x轴
df_long <- df_poisson %>%
  mutate(observation_id = row_number()) %>%
  pivot_longer(cols = V1:V17, names_to = "variable", values_to = "value")

步骤2:绘制17条折线

ggplot(df_long, aes(x = observation_id, y = value, color = variable)) +
  geom_line(linewidth = 1) +
  geom_point(size = 2) +  # 加点让折线更易读
  labs(title = "17 Variables Observation Line Plot",
       x = "Observation Index",
       y = "Value",
       color = "Variable") +
  theme_minimal() +
  theme(legend.position = "bottom")  # 图例放底部,避免遮挡折线

⚠️ 注意:V14到V17全为0,它们的折线会和x轴重合,几乎看不到。如果觉得17条线太拥挤,可以用分面拆分每个变量的图:

ggplot(df_long, aes(x = observation_id, y = value)) +
  geom_line(linewidth = 1, color = "#2c3e50") +
  geom_point(size = 2, color = "#e74c3c") +
  facet_wrap(~variable, scales = "free_y") +  # y轴自由缩放,适配每个变量的数值范围
  labs(title = "17 Variables Line Plots (Faceted)",
       x = "Observation Index",
       y = "Value") +
  theme_minimal()

场景2:绘制每个变量的Poisson拟合折线

如果你的需求是基于每个变量的均值(lambda)拟合Poisson分布,绘制概率质量函数(PMF)的折线,代码如下:

# 计算每个变量的lambda(均值)
lambda_df <- df_poisson %>%
  summarise(across(V1:V17, mean)) %>%
  pivot_longer(cols = everything(), names_to = "variable", values_to = "lambda")

# 生成Poisson分布的x值范围(覆盖所有可能的计数)
max_count <- max(df_poisson)
x_vals <- 0:ceiling(max_count * 1.2)

# 生成每个变量的Poisson概率数据
poisson_fit_df <- expand.grid(variable = unique(lambda_df$variable), x = x_vals) %>%
  left_join(lambda_df, by = "variable") %>%
  mutate(probability = dpois(x, lambda))

# 绘制Poisson拟合折线
ggplot(poisson_fit_df, aes(x = x, y = probability, color = variable)) +
  geom_line(linewidth = 1) +
  labs(title = "Poisson Distribution Fits for 17 Variables",
       x = "Count",
       y = "Probability",
       color = "Variable") +
  theme_minimal() +
  theme(legend.position = "right")

⚠️ 注意:V14到V17的lambda为0,Poisson分布在lambda=0时只有x=0的概率为1,所以它们的折线是x=0处的一条竖线,几乎看不到。


内容的提问来源于stack exchange,提问作者user2761073

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:03:47