1

I have a dataset named dataset as described below:

I am plotting the density plot using ggplot2.

I want to get those observations of the two group which overlaps in density plot.

Observations B Group
Obs1 4 Reference
Obs2 3 Reference
Obs3 5 Reference
Obs4 3 Reference
Obs5 3 Reference
Obs6 3 User
Obs7 3 User
Obs8 2 User
Obs9 2 User
Obs10 3 User

I have used these codes to plot the density plot: `

library(ggplot2) 
library(plyr)
library(ggpubr)


#Importing dataset#
density_data <- read.csv("dataset.csv")



#Creating density plot between ref and user data, variable B#
g2 <- ggplot(density_data, aes(x=B, color=Group, fill= Group)) +
  geom_density(color="black", alpha=0.9) + labs(x= "B", y= "Density")
g2

` the output plot is like that: Density plot between groups

  • 1
    I'm not sure I understand what you mean by 'those observations of the two group which overlaps in density plot'. – teunbrand Oct 27 '22 at 09:29
  • Is your data in long-form format? I think your dataframe may be missing an identifier variable. – jrcalabrese Oct 27 '22 at 18:04
  • @teunbrand the question has been edited with column observation, in the plot I want to know which observations, obs1, obs2 etc. of ref or user groups are under the overlaping density. The observation table is long dataframe table in which user obseravtions number and their value for varibale "B" will be change according to user input. – Sandeep Saini Oct 29 '22 at 16:14
  • @jrcalabrese the question has been edited with column observation, in the plot I want to know which observations, obs1, obs2 etc. of ref or user groups are under the overlaping density. The observation table is long dataframe table in which user obseravtions number and their value for varibale "B" will be change according to user input. – Sandeep Saini Oct 29 '22 at 16:15

1 Answers1

0

Another way of visualizing your data is with a barplot. Below is code for three visualizations: your histogram, a barplot, and a barplot with labels. Now your ID numbers are part of the visualization. Stacked labels from this SO post.

library(tidyverse)

df <- structure(list(Observations = c("Obs1", "Obs2", "Obs3", "Obs4", 
                                      "Obs5", "Obs6", "Obs7", "Obs8", 
                                      "Obs9", "Obs10"), 
                     B = c(4L, 3L, 5L, 3L, 3L, 3L, 3L, 2L, 2L, 3L), 
                     Group = c("Reference", "Reference", "Reference", 
                               "Reference", "Reference", 
                               "User", "User", "User", "User", "User")), 
                class = "data.frame", row.names = c(NA, -10L))

ggplot(df, aes(x = B, color = Group, fill = Group)) +
  geom_density(color = "black", alpha = 0.9)

ggplot(df, aes(x = B, fill = Group)) +
  geom_bar()

# barplot but now with labels
# I only exported an image for this plot
ggplot(df, aes(x = B, fill = Group)) +
  geom_bar() +
  geom_text(aes(label = Observations, y = 1), 
            position = position_stack(vjust = 0.5), 
            color = "black", size = 3.5)

enter image description here

jrcalabrese
  • 2,184
  • 3
  • 10
  • 30
  • The number of user observations will be 1000 maximum and 100 minimum and the user observation will be merge in to a single dataframe with reference and it will be dynamic according to user input (100 to 1000)..so Iam worried a a barplot would be best for that purpose. is it possible to reterieve the overlap observations from the dataframe using ggplot_build? – Sandeep Saini Oct 29 '22 at 16:44
  • Using the sample data you've provided, can you edit your question to include what you want your desired final output to look like? Here is an example of a user who posted both the code they currently had and what they wanted their desired final output to look like: https://stackoverflow.com/questions/74238885/combine-migration-in-and-out-data-by-different-common-columns – jrcalabrese Oct 29 '22 at 16:56