0

I want a pie chart instead of a table

method <- dataframe (stringsFactors=FALSE,method=c("violent","nonviolent"),accessiblepeople = (20000,60000)

usage <- dataframe (stringsFactors=FALSE,method=c("violent","nonviolent"),usage = (30%,70%)

ggplot(method, aes(x=method,y=accessiblepeople)+geom_bar(stat="identity")+ggtitle("")+ggpiechart())

Problem add piechart seems not working

harre
  • 7,081
  • 2
  • 16
  • 28
  • 1
    Never heard of `ggpiechart`. Where is that from? + I am afraid your code is not working (missing parenthesis etc.). Check this post on how to make a pie chart with ggplot: https://stackoverflow.com/questions/47238098/plotting-pie-charts-in-ggplot2 – harre Aug 04 '22 at 09:55
  • I want to add a piechart next to the ggplot, that is all –  Aug 04 '22 at 10:12
  • would tat work, that in one half there is a piechart and on the other hand histogram? –  Aug 04 '22 at 10:16

1 Answers1

0

You can use plot_grid from the package cowplot to place plots next to each other. I also specified a few features with theme() to remove the gray background so the final plot looks more cohesive.

library(tidyverse)
library(cowplot)

method <- data.frame(method = c("violent", "nonviolent"), 
                     accessiblepeople = c(20000,60000))

bar <- ggplot(method, aes(x = method, y = accessiblepeople, fill = method)) + 
  geom_bar(stat = "identity") + ggtitle("") + 
  theme_bw() +
  theme(legend.position = "none") + 
  theme(panel.border = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), 
        axis.line = element_line(color = "black"))

pie <- ggplot(method_df, aes(x="", y=accessiblepeople, fill=method)) +
  geom_bar(stat="identity", width=1) +
  coord_polar("y", start=0) + theme_void()

plot_grid(bar, pie)

enter image description here

jrcalabrese
  • 2,184
  • 3
  • 10
  • 30