R adding a datatable to a ggplot graph using viewPorts : Scaling the Grob

后端 未结 3 1902
情深已故
情深已故 2020-12-30 06:17

I\'m trying to add a data-table to a graph made in ggplot (similar to the excel functionality but with the flexibility to change the axis its on)

I\'ve had a few goe

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-30 06:21

    IMHO this is not a well designed graphic. Firstly, I don't understand why you need a zero origin when the values range from 300 to 500, which is a disguised way of saying that I don't like the bar chart metaphor. You're also attempting to use bar fill to represent differences in the values of c, of which there are only six. Here's what I believe is a simpler approach to the problem. Given your xta data,

    # Convert the categories to a factor
    xta$N <- factor(xta$n, levels = xta$n)
    
    # Simple approach:
    ggplot(xta, aes(x = f, y = N, color = factor(c))) +
       geom_point() +
       labs(color = "c") +
       geom_text(aes(x = 575, label = round(f, 1)), size = 4, color = "black")
    

    That's not an interesting graphic to me. What might add a little insight, depending on the context of the problem, would be to sort the responses in increasing order and add a size aesthetic to punctuate differences between levels of c. (You could also use size without color.) Finally, because we put the factor levels on the vertical axis so that their labels are clearly visible, we can also insert the f values as text by extending the horizontal axis a bit.

    ggplot(xta, aes(x = f, y = reorder(N, f), color = factor(c), size = c)) + 
       geom_point() +
       labs(color = "c") +
       geom_text(aes(x = 575, label = round(f, 1)), size = 4, color = "black")
    

    There are enough hints in this code for you to take it in a different direction. I'll leave that up to you.

提交回复
热议问题