如何在通过ggarrange创建的图表周围添加边框,并增加组合图表之间的空间

如何在通过ggarrange创建的图表周围添加边框,并增加组合图表之间的空间,r,ggplot2,ggpubr,R,Ggplot2,Ggpubr,我试图绘制Numa节点的CPU和内存使用情况 在我的示例中有4个numa节点,每个节点都有CPU和内存使用率图表 问题是-在每个节点中可能有许多CPU值(每个核1个条,在我的示例中为20个核),而只有一个内存值,因此我必须使用不同宽度的图表(CPU图表比内存图表宽)。因为注释文本(NUMA XX)看起来不像是现在图表的中间部分。我真的说不出是哪个节点的内存条 我想我需要使用边框或背景色将numa节点彼此分开,这样每个numa节点的CPU和内存就在一起了。另外,我认为我需要减少numa节点内CP

我试图绘制Numa节点的CPU和内存使用情况

在我的示例中有4个numa节点,每个节点都有CPU和内存使用率图表

问题是-在每个节点中可能有许多CPU值(每个核1个条,在我的示例中为20个核),而只有一个内存值,因此我必须使用不同宽度的图表(CPU图表比内存图表宽)。因为注释文本(NUMA XX)看起来不像是现在图表的中间部分。我真的说不出是哪个节点的内存条

我想我需要使用边框或背景色将numa节点彼此分开,这样每个numa节点的CPU和内存就在一起了。另外,我认为我需要减少numa节点内CPU和内存之间的空间,并增加numa节点之间的空间。怎么做

我的代码:

library(dplyr)

library(ggplot2)

library(ggpubr)

library(tidyr)

memory_df <- structure(list(NumaNode = c("00", "00", "01", "01", "02", "02", "03", "03"),
                         CounterName = c("TotalMemory", "UsedMemory", "TotalMemory", 
                                      "UsedMemory", "TotalMemory", "UsedMemory", "TotalMemory", "UsedMemory"),
                    Value = c(48, 45.55, 48, 46.38, 48, 46.54, 48, 47.39)),
                    row.names = c(NA, -8L),
                    class = c("tbl_df", "tbl", "data.frame"))

memory_y_scale <-  memory_df %>% filter(CounterName=='TotalMemory') %>% select (Value) %>% max() + 6

cpu_df <- structure(list(NumaNode = c("00", "00", "00", "00", "00", "00", 
                                      "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
                                      "00", "00", "00", "01", "01", "01", "01", "01", "01", "01", "01", 
                                      "01", "01", "01", "01", "01", "01", "01", "01", "01", "01", "01", 
                                      "01"), Counter = c("00", "01", "10", "11", "12", "13", "14", 
                                                         "15", "16", "17", "18", "19", "02", "03", "04", "05", "06", "07", 
                                                         "08", "09", "00", "01", "10", "11", "12", "13", "14", "15", "16", 
                                                         "17", "18", "19", "02", "03", "04", "05", "06", "07", "08", "09"
                                      ), Value = c(10.79, 5.85, 9.55, 9.71, 9.77, 4.73, 4.62, 10.92, 
                                                   3.55, 10.3, 1.38, 6.28, 5.42, 4.45, 10.09, 3.42, 10.14, 4.34, 
                                                   9.48, 4.17, 5.2, 3.85, 10.17, 8.49, 10.36, 8.06, 10.01, 8.42, 
                                                   9.12, 7.6, 1.98, 2.41, 4.72, 3.19, 4.74, 3.66, 4.28, 9.83, 5.78, 
                                                   10.18)), row.names = c(NA, -40L), class = c("tbl_df", "tbl", 
                                                                                               "data.frame"))



# plot function =======================================================================

plot_numa = function(num){
  
  # memory plot =====================
  
  memory_filtered_df = memory_df %>% filter(str_detect(NumaNode, num))
  
  memory_plot  <-  memory_filtered_df %>%
    filter(str_detect(CounterName, "Memory")) %>%
    pivot_wider(names_from = CounterName, values_from = Value) %>%
    ggplot(aes(x = "") ) +
    geom_col(aes(y = TotalMemory), fill = "white", color = "black") +
    geom_col(aes(y = UsedMemory), fill = "#00FF66FF", color = "black") +
    geom_text(aes(label = paste(TotalMemory, "GB"), y = TotalMemory + 5 ), color = "black") +
    geom_text(aes(
      label = paste(UsedMemory, "GB"),
      y = ifelse((TotalMemory-UsedMemory > 10), UsedMemory + 5, UsedMemory - 4)),
      color = "black") +
    theme_bw() +
    ylim(0, memory_y_scale) +
    labs(x = "Memory", y = "")
  
  # cpu plot =====================
  
  cpu_filtered_df <-  cpu_df %>% filter(str_detect(NumaNode, num))
  
  if (nrow (cpu_filtered_df) == 0 )    {
    cpu_filtered_df <- cpu_df %>% filter(str_detect(NumaNode, "00"))
  }
  
  cpu_plot <-  cpu_filtered_df %>%
    ggplot(aes(x = Counter)) +
    geom_col(aes(y = 100), fill = "white", color = "white", alpha = 0) +
    geom_col(aes(y = Value), fill = "#00AFBB", color = "black") +
    theme_bw() +
    labs(x = "CPU, % Processor Time", y = "") 
  
  
  ggpubr::ggarrange(cpu_plot, memory_plot, ncol = 2, widths = c(4,1)) %>% 
    ggpubr::annotate_figure(top = paste("        NUMA",num))
  
}

numa_numbers <- unique(memory_df$NumaNode)

ggpubr::ggarrange(plotlist = map(.x = numa_numbers, .f = ~plot_numa(num = .x)))

库(dplyr)
图书馆(GG2)
图书馆(ggpubr)
图书馆(tidyr)
内存密度%select(值)%%>%max()+6
cpu_df%过滤器(str_检测(NumaNode,num))
内存\u绘图%
筛选器(str_detect(计数器名,“内存”))%%
pivot\u加宽(名称\u from=CounterName,值\u from=Value)%>%
ggplot(aes(x=”“))+
geom_col(aes(y=TotalMemory),fill=“白色”,color=“黑色”)+
geom_col(aes(y=UsedMemory),fill=“#00FF66FF”,color=“black”)+
几何图形文本(aes(标签=粘贴(TotalMemory,GB),y=TotalMemory+5),颜色=“黑色”)+
geom_文本(aes)(
标签=粘贴(使用的内存,“GB”),
y=ifelse((总内存UsedMemory>10)、UsedMemory+5、UsedMemory-4)),
color=“黑色”)+
主题_bw()+
ylim(0,内存大小)+
实验室(x=“内存”,y=”“)
#cpu绘图=====================
cpu_filtered_df%过滤器(str_检测(numode,num))
如果(nrow(cpu\u过滤的\u df)==0){
cpu_已过滤_df%过滤器(str_检测(NumaNode,“00”))
}
cpu_绘图%
ggplot(aes(x=计数器))+
几何图形颜色(aes(y=100),fill=“白色”,color=“白色”,alpha=0)+
geom_col(aes(y=值),fill=“#00AFBB”,color=“黑色”)+
主题_bw()+
实验室(x=“CPU,%Processor Time”,y=”“)
ggpubr::ggarrange(cpu_图,内存_图,ncol=2,宽度=c(4,1))%>%
ggpubr::注释_图形(顶部=粘贴(“NUMA”,num))
}

numa_编号请使用
plot.margin
inside
theme()
查找以下一些代码,用于间隔部分。我还更改了位行76,
widths
ggpubr::ggarrange(cpu\u图,内存\u图,ncol=2,widths=c(2,1))%%>%

library(ggpubr)
library(tidyverse)

memory_df <- structure(list(NumaNode = c("00", "00", "01", "01", "02", "02", "03", "03"),
                            CounterName = c("TotalMemory", "UsedMemory", "TotalMemory", 
                                            "UsedMemory", "TotalMemory", "UsedMemory", "TotalMemory", "UsedMemory"),
                            Value = c(48, 45.55, 48, 46.38, 48, 46.54, 48, 47.39)),
                       row.names = c(NA, -8L),
                       class = c("tbl_df", "tbl", "data.frame"))

memory_y_scale <-  memory_df %>% filter(CounterName=='TotalMemory') %>% select (Value) %>% max() + 6

cpu_df <- structure(list(NumaNode = c("00", "00", "00", "00", "00", "00", 
                                      "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
                                      "00", "00", "00", "01", "01", "01", "01", "01", "01", "01", "01", 
                                      "01", "01", "01", "01", "01", "01", "01", "01", "01", "01", "01", 
                                      "01"), Counter = c("00", "01", "10", "11", "12", "13", "14", 
                                                         "15", "16", "17", "18", "19", "02", "03", "04", "05", "06", "07", 
                                                         "08", "09", "00", "01", "10", "11", "12", "13", "14", "15", "16", 
                                                         "17", "18", "19", "02", "03", "04", "05", "06", "07", "08", "09"
                                      ), Value = c(10.79, 5.85, 9.55, 9.71, 9.77, 4.73, 4.62, 10.92, 
                                                   3.55, 10.3, 1.38, 6.28, 5.42, 4.45, 10.09, 3.42, 10.14, 4.34, 
                                                   9.48, 4.17, 5.2, 3.85, 10.17, 8.49, 10.36, 8.06, 10.01, 8.42, 
                                                   9.12, 7.6, 1.98, 2.41, 4.72, 3.19, 4.74, 3.66, 4.28, 9.83, 5.78, 
                                                   10.18)), row.names = c(NA, -40L), class = c("tbl_df", "tbl", 
                                                                                               "data.frame"))



# plot function =======================================================================

plot_numa = function(num){
  
  # memory plot =====================
  
  memory_filtered_df = memory_df %>% filter(str_detect(NumaNode, num))
  
  memory_plot  <-  memory_filtered_df %>%
    filter(str_detect(CounterName, "Memory")) %>%
    pivot_wider(names_from = CounterName, values_from = Value) %>%
    ggplot(aes(x = "") ) +
    geom_col(aes(y = TotalMemory), fill = "white", color = "black") +
    geom_col(aes(y = UsedMemory), fill = "#00FF66FF", color = "black") +
    geom_text(aes(label = paste(TotalMemory, "GB"), y = TotalMemory + 5 ), color = "black") +
    geom_text(aes(
      label = paste(UsedMemory, "GB"),
      y = ifelse((TotalMemory-UsedMemory > 10), UsedMemory + 5, UsedMemory - 4)),
      color = "black") +
    theme_bw() +
    ylim(0, memory_y_scale) +
    labs(x = "Memory", y = "") +
    theme(plot.margin = unit(c(0.01,2,2,0.01), "cm"))
  
  # cpu plot =====================
  
  cpu_filtered_df <-  cpu_df %>% filter(str_detect(NumaNode, num))
  
  if (nrow (cpu_filtered_df) == 0 )    {
    cpu_filtered_df <- cpu_df %>% filter(str_detect(NumaNode, "00"))
  }
  
  cpu_plot <-  cpu_filtered_df %>%
    ggplot(aes(x = Counter)) +
    geom_col(aes(y = 100), fill = "white", color = "white", alpha = 0) +
    geom_col(aes(y = Value), fill = "#00AFBB", color = "black") +
    theme_bw() +
    labs(x = "CPU, % Processor Time", y = "") +
    theme(plot.margin = unit(c(0.01,0.01,2,0), "cm"))
  
  
  ggpubr::ggarrange(cpu_plot, memory_plot, ncol = 2, widths = c(2,1)) %>% 
    ggpubr::annotate_figure(top = paste("        NUMA",num))
  
}

numa_numbers <- unique(memory_df$NumaNode)

ggpubr::ggarrange(plotlist = map(.x = numa_numbers, .f = ~plot_numa(num = .x)))
库(ggpubr)
图书馆(tidyverse)
内存密度%select(值)%%>%max()+6
cpu_df%过滤器(str_检测(NumaNode,num))
内存\u绘图%
过滤器(str_detect(计数器名,“内存”))%>%
pivot\u加宽(名称\u from=CounterName,值\u from=Value)%>%
ggplot(aes(x=”“))+
geom_col(aes(y=TotalMemory),fill=“白色”,color=“黑色”)+
geom_col(aes(y=UsedMemory),fill=“#00FF66FF”,color=“black”)+
几何图形文本(aes(标签=粘贴(TotalMemory,GB),y=TotalMemory+5),颜色=“黑色”)+
geom_文本(aes)(
标签=粘贴(使用的内存,“GB”),
y=ifelse((总内存UsedMemory>10)、UsedMemory+5、UsedMemory-4)),
color=“黑色”)+
主题_bw()+
ylim(0,内存大小)+
实验室(x=“内存”,y=”“)+
主题(plot.margin=单位(c(0.01,2,2,0.01),“cm”))
#cpu绘图=====================
cpu_filtered_df%过滤器(str_检测(numode,num))
如果(nrow(cpu\u过滤的\u df)==0){
cpu_已过滤_df%过滤器(str_检测(NumaNode,“00”))
}
cpu_绘图%
ggplot(aes(x=计数器))+
几何图形颜色(aes(y=100),fill=“白色”,color=“白色”,alpha=0)+
geom_col(aes(y=值),fill=“#00AFBB”,color=“黑色”)+
主题_bw()+
实验室(x=“CPU,%Processor Time”,y=”“)+
主题(plot.margin=单位(c(0.01,0.01,2,0),“cm”))
ggpubr::ggarrange(cpu_图,内存_图,ncol=2,宽度=c(2,1))%>%
ggpubr::注释_图形(顶部=粘贴(“NUMA”,num))
}

numa#U数字我有点不清楚你在问什么,你想让“numa”集中在CPU和内存图表之上吗?或者只是把它放在CPU图表的中心?@Jonni添加了图片。您好:)您是否尝试在
theme()中使用
plot.margin
参数?例如:
主题(plot.margin=单位(c(1,2,2,1),“cm”)
。这会在绘图周围添加空间。此外,如果您真的想在绘图周围添加边框,也许您可以按照这里的建议添加“NULL”绘图:看起来不错,但现在NUMA内部的CPU和内存条之间有很多空间。它们看起来仍然不在一起。@Maxim您可以使用传递给
绘图的值。margin
可根据需要进行更改。@Maxim,更改了margin可考虑您的评论,请查看更新的答案。是否可以使用背景色?每个节点-白色背景,间隔-白色?@Maxim,我不知道。请务必记住,您有可以使用
theme()
函数编辑的绘图,以及将这些绘图放在“空白”页面上的函数。如果要更改绘图背景,可以使用
主题(plot.background=element_rect(fill=“yourcolor”)
。但我不知道如何改变收集这些情节的“页面”的背景。