R绘制日期数据的多个geom_line

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

R plot date data multiple geom_line

问题

绘制以下数据。 尝试在图中包括每种技术的线条,不使用facetwrap。绘制技术和文件数量 -

日期 emmm 任务 coll_task 技术 文件数
2023-06-01 vnmenm1 tr e3g_tr_vnmenm1 3g 1136
2023-06-01 vnmenm1 tr e4g_tr_vnmenm1 4g 3475
2023-06-01 vnmsgsn1 tr e4g_tr_vnmsgsn1 4g 317
2023-06-03 vnmenm1 tr e3g_tr_vnmenm1 3g 1136
2023-06-03 vnmenm1 tr e4g_tr_vnmenm1 4g 8899
2023-06-03 vnmsgsn1 tr e4g_tr_vnmsgsn1 4g 296
2023-06-04 vnmenm1 tr e3g_tr_vnmenm1 3g 1136
2023-06-04 vnmenm1 tr e4g_tr_vnmenm1 4g 9034
2023-06-04 vnmsgsn1 tr e4g_tr_vnmsgsn1 4g 292

使用以下代码 -

data %>% group_by(Date, emmm, tech, filescount) %>%  summarize(filescount = sum(filescount)) %>% ggplot(aes(Date, emmm, tech, color = filescount)) + geom_point(size = 2.4, alpha = 0.5) + geom_line(aes(x = Date, y = emmm), size = 1,  alpha = 0.4, stat = "identity", na.rm = TRUE) 

生成如下图像 -
R绘制日期数据的多个geom_line

有没有办法同时显示技术和文件数量,即 - 按技术和文件数量绘制geom_line!

英文:

Plotting the below data. trying to include the lines per tech, Without using the facetwrap.
Plot both tech and files per enm --

Date emmm task coll_task tech filescount
2023-06-01 vnmenm1 tr e3g_tr_vnmenm1 3g 1136
2023-06-01 vnmenm1 tr e4g_tr_vnmenm1 4g 3475
2023-06-01 vnmsgsn1 tr e4g_tr_vnmsgsn1 4g 317
2023-06-03 vnmenm1 tr e3g_tr_vnmenm1 3g 1136
2023-06-03 vnmenm1 tr e4g_tr_vnmenm1 4g 8899
2023-06-03 vnmsgsn1 tr e4g_tr_vnmsgsn1 4g 296
2023-06-04 vnmenm1 tr e3g_tr_vnmenm1 3g 1136
2023-06-04 vnmenm1 tr e4g_tr_vnmenm1 4g 9034
2023-06-04 vnmsgsn1 tr e4g_tr_vnmsgsn1 4g 292

Using below code -

data %>% group_by(Date, emmm, tech, filescount) %>%  summarize(filescount = sum(filescount)) %>% ggplot(aes(Date, emmm, tech, color = filescount)) + geom_point(size = 2.4, alpha = 0.5) + geom_line(aes(x = Date, y = emmm), size = 1,  alpha = 0.4, stat = "identity", na.rm = TRUE) 

gives an image like -
R绘制日期数据的多个geom_line

Any way to present with both tech and showing the filescount, i.e - geom_line per tech and filescount !

答案1

得分: 1

library(ggplot2)
library(dplyr)

# 作为数据框的给定测试数据
df <- data.frame(
    Date = as.Date(c("2023-06-01", "2023-06-01", "2023-06-01", "2023-06-03", "2023-06-03", "2023-06-03", "2023-06-04", "2023-06-04", "2023-06-04")),
    emmm = c("vnmenm1", "vnmenm1", "vnmsgsn1", "vnmenm1", "vnmenm1", "vnmsgsn1", "vnmenm1", "vnmenm1", "vnmsgsn1"),
    task = rep("tr", 9),
    coll_task = c("e3g_tr_vnmenm1", "e4g_tr_vnmenm1", "e4g_tr_vnmsgsn1", "e3g_tr_vnmenm1", "e4g_tr_vnmenm1", "e4g_tr_vnmsgsn1", "e3g_tr_vnmenm1", "e4g_tr_vnmenm1", "e4g_tr_vnmsgsn1"),
    tech = c("3g", "4g", "4g", "3g", "4g", "4g", "3g", "4g", "4g"),
    filescount = c(1136, 3475, 317, 1136, 8899, 296, 1136, 9034, 292)
)

# 创建一个用于绘图的数据框
df_sum <- df %>%
    group_by(emmm, tech, Date) %>%
    summarize(total_files = sum(filescount), .groups = 'drop')

# 绘制数据
ggplot(df_sum, aes(x = Date, y = total_files, group = interaction(emmm, tech), color = interaction(emmm, tech), linetype = interaction(emmm, tech))) +
    geom_line() +
    geom_point() +
    labs(x = "Date", y = "Files per Tech", color = "Tech", linetype = "Tech") +
    scale_color_discrete(name = "Emmm - Tech", labels = c("vnmenm1 - 3g", "vnmenm1 - 4g", "vnmsgsn1 - 4g")) +
    scale_linetype_discrete(name = "Emmm - Tech", labels = c("vnmenm1 - 3g", "vnmenm1 - 4g", "vnmsgsn1 - 4g")) +
    theme_minimal()

输出:

R绘制日期数据的多个geom_line

英文:
library(ggplot2)
library(dplyr)

# Your given test data as a dataframe
df &lt;- data.frame(
    Date = as.Date(c(&quot;2023-06-01&quot;, &quot;2023-06-01&quot;, &quot;2023-06-01&quot;, &quot;2023-06-03&quot;, &quot;2023-06-03&quot;, &quot;2023-06-03&quot;, &quot;2023-06-04&quot;, &quot;2023-06-04&quot;, &quot;2023-06-04&quot;)),
    emmm = c(&quot;vnmenm1&quot;, &quot;vnmenm1&quot;, &quot;vnmsgsn1&quot;, &quot;vnmenm1&quot;, &quot;vnmenm1&quot;, &quot;vnmsgsn1&quot;, &quot;vnmenm1&quot;, &quot;vnmenm1&quot;, &quot;vnmsgsn1&quot;),
    task = rep(&quot;tr&quot;, 9),
    coll_task = c(&quot;e3g_tr_vnmenm1&quot;, &quot;e4g_tr_vnmenm1&quot;, &quot;e4g_tr_vnmsgsn1&quot;, &quot;e3g_tr_vnmenm1&quot;, &quot;e4g_tr_vnmenm1&quot;, &quot;e4g_tr_vnmsgsn1&quot;, &quot;e3g_tr_vnmenm1&quot;, &quot;e4g_tr_vnmenm1&quot;, &quot;e4g_tr_vnmsgsn1&quot;),
    tech = c(&quot;3g&quot;, &quot;4g&quot;, &quot;4g&quot;, &quot;3g&quot;, &quot;4g&quot;, &quot;4g&quot;, &quot;3g&quot;, &quot;4g&quot;, &quot;4g&quot;),
    filescount = c(1136, 3475, 317, 1136, 8899, 296, 1136, 9034, 292)
)

# Create a dataframe to plot
df_sum &lt;- df %&gt;%
    group_by(emmm, tech, Date) %&gt;%
    summarize(total_files = sum(filescount), .groups = &#39;drop&#39;)

# Plot the data
ggplot(df_sum, aes(x = Date, y = total_files, group = interaction(emmm, tech), color = interaction(emmm, tech), linetype = interaction(emmm, tech))) +
    geom_line() +
    geom_point() +
    labs(x = &quot;Date&quot;, y = &quot;Files per Tech&quot;, color = &quot;Tech&quot;, linetype = &quot;Tech&quot;) +
    scale_color_discrete(name = &quot;Emmm - Tech&quot;, labels = c(&quot;vnmenm1 - 3g&quot;, &quot;vnmenm1 - 4g&quot;, &quot;vnmsgsn1 - 4g&quot;)) +
    scale_linetype_discrete(name = &quot;Emmm - Tech&quot;, labels = c(&quot;vnmenm1 - 3g&quot;, &quot;vnmenm1 - 4g&quot;, &quot;vnmsgsn1 - 4g&quot;)) +
    theme_minimal()

Output:

R绘制日期数据的多个geom_line

huangapple
  • 本文由 发表于 2023年6月6日 05:46:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410202.html
匿名

发表评论

匿名网友

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

确定