Maxim
Maxim

Reputation: 281

How to use one value as Y-max and other as fill in ggplot

I need to show memory usage in GB on numa nodes of host.

Here is my example tibble:

library(tidyr)

numa.nodes <- tibble (
  numa_name = c("numa_01","numa_01","numa_01","numa_01","numa_01","numa_01","numa_02","numa_02","numa_02","numa_02"),
  counter_name =c("cpu01","cpu02","cpu03","cpu04","memory_used","memory_total","cpu01","cpu02","memory_used","memory_total"),
    value = c(sample(0:100,4), sample(0:32,1), 32, sample(0:100,1), sample(0:100,1), sample(0:128,1), 128)
)

numa.nodes <- numa.nodes %>% add_row(
  numa_name = c("numa_03","numa_03","numa_03","numa_03","numa_03","numa_03","numa_04","numa_04","numa_04","numa_04"),
  counter_name =c("cpu01","cpu02","cpu03","cpu04","memory_used","memory_total","cpu01","cpu02","memory_used","memory_total"),
  value = c(sample(0:100,4), sample(0:32,1), 32, sample(0:100,1), sample(0:100,1), sample(0:128,1), 128)
  )


numa.nodes <- numa.nodes %>% mutate(counter_name=factor(counter_name,levels = unique(counter_name),ordered = T))

Here is how I try in ggplot2:

numa.nodes %>% filter(counter_name == c("memory_used", "memory_total")) %>% 
  ggplot() +
  aes(x = counter_name, y = value, label = value) +
  geom_bar(stat = 'identity') +
  facet_wrap(vars(numa_name), strip.position = 'bottom', scales = "free_x")+
  theme_bw()+
  geom_text(size = 3, position = position_stack(vjust = 0.5)) +
  theme(strip.placement = 'outside',
        strip.background = element_blank(),
        legend.position = 'top',
        axis.text = element_text(color='black',face='bold'),
        axis.title = element_text(color='black',face='bold'),
        legend.text = element_text(color='black',face='bold'),
        legend.title = element_text(color='black',face='bold'),
        strip.text = element_text(color='black',face='bold')) +
        labs(x='Memory',y="Usage %")

Here is my result plot:

my result plot

here is what I need:

what I need

How to make it in ggplot2 or any other lib?

Upvotes: 1

Views: 58

Answers (2)

AndreasM
AndreasM

Reputation: 952

You could do something like this. Layout of facets and label placing etc. is not yet ideal. But maybe this helps...

#total memory only
mem.total <- numa.nodes[numa.nodes$counter_name == "memory_total",]

#memory used vs. unused
mem.usage <- numa.nodes[numa.nodes$counter_name == "memory_used",]
mem.usage <- rbind(mem.usage,
                   data.frame(numa_name = mem.total$numa_name,
                              counter_name = "memory_unused",
                              value = mem.total$value - mem.usage$value))
mem.usage$counter_name = factor(mem.usage$counter_name, 
                                levels = c("memory_unused", "memory_used"))

#plot
ggplot(data = mem.usage, aes(x = numa_name, y = value, label = value)) +
  facet_wrap(~ numa_name, scales = "free_x") +
  geom_bar(aes(group = counter_name, fill = counter_name),
           stat = 'identity', position = "stack", color = "black") +
  geom_label(data = mem.total, size = 5) +
  geom_label(data = mem.usage[mem.usage$counter_name == "memory_used",], 
             size = 5) +
  scale_fill_manual(values = c("white", "black")) +
  theme_void() +
  theme(legend.position = "top", legend.title = element_blank())
  

enter image description here

Upvotes: 0

Duck
Duck

Reputation: 39613

Try this. If I understand correctly you want to plot the difference between total memory and used memory. In that case you have to reshape the data to wide compute the difference and then sketch the plot. Here the code:

library(tidyverse)
#Code
numa.nodes %>% filter(counter_name == c("memory_used", "memory_total")) %>%
  pivot_wider(names_from = counter_name,values_from=value) %>%
  mutate(Diff=memory_total-memory_used) %>% select(-memory_used) %>%
  ggplot(aes(x=numa_name,y=memory_total)) +
  geom_bar(stat = 'identity',aes(fill='memory_total'),color='black')+
  geom_bar(stat = 'identity',aes(y=Diff,fill='Diff'),color='black') +
  facet_wrap(vars(numa_name), strip.position = 'bottom', scales = "free")+
  theme_bw()+
  geom_text(aes(y=memory_total,
                label=memory_total),size = 3) +
  geom_text(aes(y=Diff,label=Diff),
            position = position_stack(vjust = 0.5),
            size=3)+
  theme(strip.placement = 'outside',
        strip.background = element_blank(),
        legend.position = 'top',
        axis.text = element_text(color='black',face='bold'),
        axis.title = element_text(color='black',face='bold'),
        legend.text = element_text(color='black',face='bold'),
        legend.title = element_text(color='black',face='bold'),
        strip.text = element_text(color='black',face='bold'),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank()) +
  labs(x='Memory',y="Usage %")+
  labs(fill='Variable')

Output:

enter image description here

Update:

#Code 2
numa.nodes %>% filter(counter_name == c("memory_used", "memory_total")) %>%
  pivot_wider(names_from = counter_name,values_from=value) %>%
  mutate(Diff=memory_total-memory_used) %>% select(-memory_total) %>%
  pivot_longer(-numa_name) %>%
  mutate(name=factor(name,levels=c('memory_used','Diff'),ordered = T)) %>%
  ggplot(aes(x=numa_name,y=value,fill=name)) +
  geom_bar(stat = 'identity',color='black')+
  facet_wrap(vars(numa_name), strip.position = 'bottom',scales = 'free_x')+
  theme_bw()+
  geom_text(aes(label=value),size = 3,position = position_stack(0.5),
            color='white',fontface='bold') +
  theme(strip.placement = 'outside',
        strip.background = element_blank(),
        legend.position = 'top',
        axis.text = element_text(color='black',face='bold'),
        axis.title = element_text(color='black',face='bold'),
        legend.text = element_text(color='black',face='bold'),
        legend.title = element_text(color='black',face='bold'),
        strip.text = element_text(color='black',face='bold'),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank()) +
  labs(x='Memory',y="Usage %")+
  labs(fill='Variable')

Output:

enter image description here

Update 2:

#Code 4
numa.nodes %>% filter(counter_name == c("memory_used", "memory_total")) %>%
  pivot_wider(names_from = counter_name,values_from=value) %>%
  ggplot(aes(x=numa_name,y=memory_total)) +
  geom_bar(stat = 'identity',aes(fill='memory_total'),color='black')+
  geom_bar(stat = 'identity',aes(y=memory_used,fill='memory_used'),color='black') +
  facet_wrap(vars(numa_name), strip.position = 'bottom', scales = "free_x")+
  theme_bw()+
  geom_text(aes(y=memory_total,
                label=memory_total),size = 3) +
  geom_text(aes(y=memory_used,label=memory_used),
            position = position_stack(vjust = 0.5),
            size=3)+
  theme(strip.placement = 'outside',
        strip.background = element_blank(),
        legend.position = 'top',
        axis.text = element_text(color='black',face='bold'),
        axis.title = element_text(color='black',face='bold'),
        legend.text = element_text(color='black',face='bold'),
        legend.title = element_text(color='black',face='bold'),
        strip.text = element_text(color='black',face='bold'),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank()) +
  labs(x='Memory',y="Usage %")+
  labs(fill='Variable')

Output:

enter image description here

Upvotes: 1

Related Questions