简体   繁体   English

R中调整图形和图例

[英]Adjust graphic and legend in R

I would like to adjust the X axis values and also my plot legend.我想调整 X 轴值以及我的 plot 图例。

Regarding the X axis, I would like the values to be up to 10, that is, [1:10].关于 X 轴,我希望值最大为 10,即 [1:10]。 It's going to 7, since my base goes to DR07, but for the sake of visualization I'd like it to go to 10.它会变成 7,因为我的基础是 DR07,但为了可视化,我希望它变成 go 到 10。

Regarding the X axis legend as you can see it is full of text.关于 X 轴图例,如您所见,它充满了文字。 But what I would like is that the X axis was written "Days" and the Y axis "Types".但我想要的是 X 轴写为“天数”,Y 轴写为“类型”。

I will insert an executable code below.我将在下面插入一个可执行代码。

library(dplyr)
library(tidyr)
library(ggplot2)
library(lubridate)

df <- structure(
  list(date = c("2021-08-01","2021-08-01","2021-08-01","2021-08-01","2021-08-01",
                "2021-08-02","2021-08-02","2021-08-02","2021-08-02","2021-08-02","2021-08-02"),
       D1 = c(0,1,0,0,5,0,1,0,0,9,4), DR01 = c(2,1,0,0,3,0,1,0,1,7,2), 
       DR02 = c(2,0,0,0,4,2,1,0,1,4,2),  DR03 = c(2,0,0,2,6,2,0,0,1,5,2),
       DR04 = c(2,0,0,5,6,2,0,0,3,7,2),  DR05 = c(2,0,0,5,6,2,0,0,7,7,2), 
       DR06 = c(2,0,0,5,7,2,0,0,7,7,1),  DR07 = c(2,0,0,6,9,2,0,0,7,8,1)), 
  class = "data.frame", row.names = c(NA, -11L))

scatter_date <- function(dt, dta = df) {
  dta %>%
    mutate(date = ymd(date)) %>%
    filter(date == ymd(dt)) %>%
    summarize(across(starts_with("DR"), sum)) %>%
    pivot_longer(everything(), names_pattern = "DR(.+)", values_to = "val") %>%
    mutate(name = as.numeric(name)) %>%
    plot(dta) 
  
}
scatter_date("2021-08-01")

在此处输入图像描述

The axis labels and x axis limits are arguments to the plot function:轴标签和 x 轴范围为 arguments 到 plot function:

library(dplyr)
library(tidyr)
library(lubridate)

scatter_date <- function(dt, dta = df) {
  dta %>%
    mutate(date = ymd(date)) %>%
    filter(date == ymd(dt)) %>%
    summarize(across(starts_with("DR"), sum)) %>%
    pivot_longer(everything(), names_pattern = "DR(.+)", values_to = "val") %>%
    mutate(name = as.numeric(name)) %>%
    plot(xlab = "Days", ylab = "Types", xlim = c(0, 10))

  
}
scatter_date("2021-08-01")

Created on 2021-08-24 by the reprex package (v2.0.0)reprex package (v2.0.0) 创建于 2021-08-24

data数据

df <- structure(
  list(date = c("2021-08-01","2021-08-01","2021-08-01","2021-08-01","2021-08-01",
                "2021-08-02","2021-08-02","2021-08-02","2021-08-02","2021-08-02","2021-08-02"),
       D1 = c(0,1,0,0,5,0,1,0,0,9,4), DR01 = c(2,1,0,0,3,0,1,0,1,7,2), 
       DR02 = c(2,0,0,0,4,2,1,0,1,4,2),  DR03 = c(2,0,0,2,6,2,0,0,1,5,2),
       DR04 = c(2,0,0,5,6,2,0,0,3,7,2),  DR05 = c(2,0,0,5,6,2,0,0,7,7,2), 
       DR06 = c(2,0,0,5,7,2,0,0,7,7,1),  DR07 = c(2,0,0,6,9,2,0,0,7,8,1)), 
  class = "data.frame", row.names = c(NA, -11L))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM