0

I'm aware that plotting in 3D is not the best way to represent data. However, my supervisor (a non statistican) has requested I do it so here I am. I'm trying to plot counts for how many documents contain singles, pairs and triples of certain key words.

I've managed to plot the data in a 2D fashion, which I'd argue is best, but for my meeting next week I'd like to be able to plot it in 3D too, even if it's to justify why plotting in 3D isn't suitable. I've attached code to reproduce my data as well as the plots I've managed to get so far.

Ideally, I would like a combination of the two plots.

2D stacked bar plot:

2D stacked bar plot

3D bar plot:

3D bar plot

My data:

b <- c(336,358,168,143,NA,358,161,142,NA,NA,168,100,NA,NA,NA,143)
c <- c(358,358,161,142,NA,3114,2200,2205,NA,NA,2200,842,NA,NA,NA,2205)
f <- c(163,161,163,100,NA,2200,2200,842,NA,NA,3388,899,NA,NA,NA,899)
e <- c(143,142,100,143,NA,2205,842,2205,NA,NA ,899,899,NA,NA,NA,2754)

ndimdata <- array(c(b, c, f, e), 
                  c(4, 4, 4), 
                  list(c("hmasm", "s", "ppng", "cdr"), 
                       c("hmasm", "s", "ppng", "cdr"), 
                       c("hmasm", "s", "ppng", "cdr")))

What the data looks like:

> ndimdata
, , hmasm

          hmasm   s ppng cdr
hmasm   336  NA   NA  NA
s       358 358   NA  NA
ppng    168 161  168  NA
cdr     143 142  100 143

, , s

       hmasm    s ppng  cdr
hmasm   358   NA   NA   NA
s       358 3114   NA   NA
ppng    161 2200 2200   NA
cdr     142 2205  842 2205

, , ppng

      hmasm    s ppng cdr
hmasm   163   NA   NA  NA
s       161 2200   NA  NA
ppng    163 2200 3388  NA
cdr     100  842  899 899

, , cdr

       hmasm    s ppng  cdr

 hmasm   143   NA   NA   NA
 s       142 2205   NA   NA
 ppng    100  842  899   NA
 cdr     143 2205  899 2754

My code:

library(ggplot2)
library(reshape2)
df <- melt(ndimdata, id.vars=c("keywords1", "keywords2"))

#2d stacked plot
ggplot(df, aes(x = Var2, y = value, fill = Var3)) + 
  geom_bar(stat = 'identity', position = 'stack') + 
  facet_grid(~ Var1)

library(latticeExtra)
#3d non-stacked plot
cloud(df$value ~ df$Var1 + df$Var3, df, 
      panel.3d.cloud = panel.3dbars, 
      xbase=0.4, ybase=0.4, scales=list(arrows=FALSE, col=1), 
      par.settings = list(axis.line = list(col = "transparent")), 
                          drape=TRUE) 
Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • Welcome to SO! Please, add some usable data to copy and paste in R (maybe using `dput(yourdata)` and posting the result), and your attempts till now, and it's going to be easier to get an answer. Also, your two image links, despite they're different, show the same plot if I'm not wrong. – s__ Feb 05 '19 at 12:55
  • You can use `plot_grid()` from the (dev version) `cowplot` package to combine the plots. See [here](https://stackoverflow.com/q/50012553/8449629). – Z.Lin Feb 07 '19 at 05:53
  • Possible duplicate of [Combining plots created by R base, lattice, and ggplot2](https://stackoverflow.com/questions/50012553/combining-plots-created-by-r-base-lattice-and-ggplot2) – Z.Lin Feb 07 '19 at 05:54

0 Answers0