Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

r - Plotting the amount of times a value was present in a column in ggplot

I'm building a Shiny application to create visualizations for output reports of a system that I have.

The report contains 3 fields I'm interested in: Address Code, Street Code and City Code. The values of these fields are numeric codes which I have, the meanings of these codes is not important for the problem, I simply want to create a plot where I can see how many times was each code present for each of the columns (a separate plot for each field)

So for example, the DF looks like this

Address Code | Street Code | City Code
   100              100        30
   100              100        30
   0                 15        40
                               50     
   25                          0

As you can see, the value of the code can also be null.

And the output I expect from this would be something like a horizontal bar plot showing

The Y axis would be the codes themselves, and the X axis would be the amount of times they were observed So following the mock table, I want this:

          ADDRESS CODES
100 --------------
75  -
50  -
25  -
0   -------

    0     1      2     3

I saw [here][1] a way to approach the issue, but I keep running into errors.

output$plot_levels <- plotly::renderPlotly({
        #loadData merely grabs the data and returns it as a data frame.
        #loadData uses fread to read the data from a file. It handles the null values as "".
        df <- loadData()
        df %>% 
            select(-`Address Code`) %>% 
            gather() %>% 
            #filter(`Address Code` != "") %>% 
            ggplot(aes(`Address Code`, ..count..)) + geom_col()
    })

I keep getting "ERROR: object 'Address Code' not found" in the app. The Address Code being referred to there is the one in the ggplot line.

So why is it saying that it doesn't exist? I already confirmed that the loadData method is reading the source file correctly, and the column name is indeed read as Address Code. [1]: Plot table objects with ggplot?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this:

library(dplyr)
library(tidyr)
library(ggplot2)
#Code
df %>% pivot_longer(everything()) %>%
  filter(!is.na(value)) %>%
  group_by(value) %>%
  summarise(N=n()) %>%
  mutate(value=factor(value,levels = unique(value),ordered = T)) %>%
  ggplot(aes(x=value,y=N))+
  geom_col(fill='cyan3')+
  coord_flip()+
  theme_bw()

Output:

enter image description here

Or using plotly:

library(dplyr)
library(tidyr)
library(ggplot2)
library(plotly)
#Code
ggplotly(df %>% pivot_longer(everything()) %>%
  filter(!is.na(value)) %>%
  group_by(value) %>%
  summarise(N=n()) %>%
  mutate(value=factor(value,levels = unique(value),ordered = T)) %>%
  ggplot(aes(x=value,y=N))+
  geom_col(fill='cyan3')+
  coord_flip()+
  theme_bw())

Output:

enter image description here

Some data used:

#Data
df <- structure(list(Address.Code = c(100L, 100L, 0L, 50L, 25L), Street.Code = c(100L, 
100L, 15L, NA, NA), City.Code = c(30L, 30L, 40L, NA, 0L)), class = "data.frame", row.names = c(NA, 
-5L))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...