0

I need to put a 3D kind of graph similar to attached image created in excel. I am not sure whether we can do this in ggplot?

cross table image

structure(list(Name = c("A", "B", "C", "D"), P = c(15089, NA, 
NA, 43083), Q = c(1589, NA, NA, 18120), R = c(93751, NA, 4709, 
211649), S = c(34167, 1323, 1520, 82378), T = c(8831, NA, 4544, 
15157)), .Names = c("Name", "P", "Q", "R", "S", "T"), row.names = c(NA, 
4L), class = "data.frame")

I have worked with this following code.

ggplot(a, aes(x = a$A, y = a$Amount, fill = a$B)) + 
geom_col(position = 'stack') + 
geom_text(aes(label = a$Amount), position = position_stack(vjust = .5),
      color='grey25', size=2) + coord_flip() 

The problem is the labels which shows on top the the graph is overlapping

Updated: Actually, I thought I need to reshape the data to achieve this kind of graph, not so sure though. So I reshaped the like below

structure(list(AA = c("A", "A", "A", "A", "A", "B", "B", "B", 
"B", "B", "C", "C", "C", "C", "C", "D", "D", "D", "D", "D"), 
    BB = c("P", "Q", "R", "S", "T", "P", "Q", "R", "S", "T", 
    "P", "Q", "R", "S", "T", "P", "Q", "R", "S", "T"), Amount = c(15089, 
    1589, 93751, 34167, 8831, NA, NA, NA, 1323, NA, NA, NA, 4709, 
    1520, 4544, 43083, 18120, 211649, 82378, 15157)), .Names = c("AA", 
"BB", "Amount"), row.names = c(NA, 20L), class = "data.frame")

I tried the following code to achieve this to which the labels are overlapping

ggplot(a, aes(x = AA, y = Amount, fill = BB)) + 
    geom_col(position = 'stack')+
    geom_text(aes(label = Amount), 
             position = position_stack(vjust = 0.2), 
              color='grey25', 
              size=2) +
    coord_flip()

Also, when I supply this to ggploty for shiny, the graph is not coming in dashboard

Lak023
  • 33
  • 1
  • 2
  • 5
  • 1
    what have you tried so far? have you checked http://ggplot2.tidyverse.org/reference/geom_bar.html ? – Jan Aug 20 '17 at 08:43
  • You could also check [here](http://www.sthda.com/english/wiki/impressive-package-for-3d-and-4d-graph-r-software-and-data-visualization) – akrun Aug 20 '17 at 08:48
  • I have worked with this following code. The problem is the labels which shows on top the the graph is overlapping.ggplot(a, aes(x = a$A, y = a$Amount, fill = a$B)) + geom_col(position = 'stack')+ geom_text(aes(label = a$Amount), position = position_stack(vjust = .5), color='grey25', size=2) + coord_flip() – Lak023 Aug 20 '17 at 09:18
  • @Lak023 your code in the comment does not match the variable names in the question. can you adapt it accordingly? – Jan Aug 20 '17 at 09:28
  • @Jan I updated the question – Lak023 Aug 20 '17 at 09:48
  • You have the graph working, but (now) your question is not about the graph, but rather how to have labels not overlap. – Roman Luštrik Aug 20 '17 at 13:37
  • @RomanLuštrik, yes the labels of BB are very near which leads to overlap of the values. This makes difficult to read the values and graph as clumpsy – Lak023 Aug 20 '17 at 13:53
  • 1
    @Lak023 check http://blog.revolutionanalytics.com/2016/01/avoid-overlapping-labels-in-ggplot2-charts.html for ggrepel which might help you or take a look at this solution (note quite what you want but maybe a solution anyway): https://stackoverflow.com/questions/16127170/how-to-prevent-two-labels-to-overlap-in-a-barchart – Jan Aug 20 '17 at 13:59

0 Answers0