akr
akr

Reputation: 1

R: Not able to plot data with length

I have a dataset like this -

content, contentid, person <br/>
course1, 1, X <br/>
course2, 2, Y <br/>
course3, 3, X <br/>

I want to calculate the number of courses a person has taken and plot it.

I have tried to subset the data using the person's name.

content, contentid, person <br/>
course1, 1, X <br/>
course, 3, X <br/>

Then I tried to calculate the number of unique contentid in the subsetted data and plot it.

var<-unique(dataset$contentid)

 hchart("column",y=length(var))

The plot shows 1 on the y scale. I want it to be 2. Please help!

Upvotes: 0

Views: 32

Answers (2)

Bruno
Bruno

Reputation: 4151

Here are two possible solutions.

Reproducible example data.

library(tidyverse)

db <- tribble(
  ~content,~contentid, ~person,  
  "course1", 1, "X", 
  "course2", 2, "Y",
  "course3", 3, "X")

Then you need to decide if want to plot or if you want the information as a table

Table

db %>%
  group_by(person) %>% 
  summarise(number_courses = n())

Plot

db %>%
  ggplot() +
  aes(person, fill = content) +
  geom_bar()

Try these out if you and tell me if I missed your point.

Upvotes: 1

akrun
akrun

Reputation: 887048

We can get the frequency by 'person' (count) and use ggplot to plot as a bar plot

library(dplyr)
library(ggplot2)
df1 %>% 
    count(person) %>% 
    ggplot(., aes(x = person, y = n)) + 
         geom_bar(stat = 'identity')

Upvotes: 1

Related Questions