ggplot not working in R markdown html document.

This code works well and produces a graph while running in r studio, but does not produce a graph in the output of R markdown Html.

ggplot(data = df3, aes(x = day, y = Avg_Annual_Member_Rides)) +
  geom_bar(stat = 'identity', fill = 'Purple') +
  labs(title = 'Each Day Average Rides for Annual Members', x = 'Days', y = 'No.of Rides') +
  geom_text(aes(label = stat(Avg_Annual_Member_Rides)), stat = 'identity', vjust = -0.3)

Please see this image for reference

What you show is certainly a graph. its a graph in which Avg_Annual_Member_Rides is equal to zero every day.
So the primary issue must be how you put df3 together, and whether you do that the same or differently than when you see a chart you are happy with.

did you put your code in an r chunk?

```{r ggplot}
ggplot(data = df3, aes(x = day, y = Avg_Annual_Member_Rides)) +
  geom_bar(stat = 'identity', fill = 'Purple') +
  labs(title = 'Each Day Average Rides for Annual Members', x = 'Days', y = 'No.of Rides') +
  geom_text(aes(label = stat(Avg_Annual_Member_Rides)), stat = 'identity', vjust = -0.3)
```

Do you see an output for the below graph using the mtcars dataset? If so there is likely a problem with loading of your data:

```{r ggplot}
ggplot(data = mtcars, aes(x = cyl, y = hp)) +
  geom_bar(stat = 'identity', fill = 'Purple') +
  labs(title = 'Title', x = 'cyl', y = 'hp')
```

This codes certainly produces a graph and is very similar to the one with issue.

ggplot(data = df4, aes(x = day, y = Avg_Casual_Member_Rides)) +
  geom_bar(stat = 'identity', fill = 'Blue') +
  labs(title = 'Each Day Average Rides for Casual Members', x = 'Days', y = 'No.of Rides') +
  geom_text(aes(label = stat(Avg_Casual_Member_Rides)), stat = 'identity', vjust = -0.3)

You can see both the graphs in the markdown document above. Any insights?

I'm sorry but I didn't understand. :grimacing:

where does df3 come from ?

df3 is a data frame I've created.

I'm confused as to why df4 (I've mentioned in another reply above) is working perfectly fine and df3 is not.
Can you please help?
Thank you in advance

Alright.... how about this :
add to your r chunk code that will print the first few rows of df3 as well as charting it.

```{r}
#peek
head(df3)

#plot
ggplot(data = df3, aes(x = day, y = Avg_Annual_Member_Rides)) +
  geom_bar(stat = 'identity', fill = 'Purple') +
  labs(title = 'Each Day Average Rides for Annual Members', x = 'Days', y = 'No.of Rides') +
  geom_text(aes(label = stat(Avg_Annual_Member_Rides)), stat = 'identity', vjust = -0.3)

Its showing 0, Why would be that ?

Meanwhile its working fine for df4 as well other data frames I'm using in this report.

its a data.frame that you created.
Thats all I know about it....

How did you create it ?
what data did you start with, and what transformations did you do on it ?

library(dplyr)
library(tidyverse)
library(ggplot2)

Imported the data set modified in spreadsheet into a data frame 'dframe'.

dframe <- read_csv('Processed_Data.csv')

Structure of Data frame

str(dframe)
colnames(dframe)
day <- c('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')
Average Rides On Each Day For Two Member Types

Created two different vectors for both Annual and Casual member types showing thier respective mean for each day.

## Annual members Average per day

Avg_Annual_Member_Rides <- c(Sunday = sum(dframe$Day_Of_Week == 'Sunday' 
                                            & dframe$Member_Type == 'annual') / 4,
                    Monday = sum(dframe$Day_Of_Week == 'Monday' 
                                            & dframe$Member_Type == 'annual') / 4,
                    Tuesday = sum(dframe$Day_Of_Week == 'Tuesday' 
                                            & dframe$Member_Type == 'annual') / 4,
                    Wednesday = sum(dframe$Day_Of_Week == 'Wednesday' 
                                            & dframe$Member_Type == 'annual') / 5,
                    Thursday = sum(dframe$Day_Of_Week == 'Thursday' 
                                            & dframe$Member_Type == 'annual') / 5,
                    Friday = sum(dframe$Day_Of_Week == 'Friday' 
                                            & dframe$Member_Type == 'annual') / 4,
                    Saturday = sum(dframe$Day_Of_Week == 'Saturday' 
                                            & dframe$Member_Type == 'annual') / 4)

  
## Casual members Average per day



Avg_Casual_Member_Rides <- c(Sunday = sum(dframe$Day_Of_Week == 'Sunday' 
                                              & dframe$Member_Type == 'casual') / 4,
                    Monday = sum(dframe$Day_Of_Week == 'Monday' 
                                              & dframe$Member_Type == 'casual') / 4,
                    Tuesday = sum(dframe$Day_Of_Week == 'Tuesday' 
                                              & dframe$Member_Type == 'casual') / 4,
                    Wednesday = sum(dframe$Day_Of_Week == 'Wednesday' 
                                              & dframe$Member_Type == 'casual') / 5,
                    Thursday = sum(dframe$Day_Of_Week == 'Thursday' 
                                              & dframe$Member_Type == 'casual') / 5,
                    Friday = sum(dframe$Day_Of_Week == 'Friday' 
                                              & dframe$Member_Type == 'casual') / 4,
                    Saturday = sum(dframe$Day_Of_Week == 'Saturday' 
                                              & dframe$Member_Type == 'casual') / 4)
                    


## Creating the new data frames

df3 <- data.frame(day,Avg_Annual_Member_Rides)

df4 <- data.frame(day,Avg_Casual_Member_Rides)

Visualization

Plotted bar graphs for both Data frames to analyze their differences and to note key points.


head(df3)


ggplot(data = df3, aes(x = day, y = Avg_Annual_Member_Rides)) +
  geom_bar(stat = 'identity', fill = 'Purple') +
  labs(title = 'Each Day Average Rides for Annual Members', x = 'Days', y = 'No.of Rides') +
  geom_text(aes(label = stat(Avg_Annual_Member_Rides)), stat = 'identity', vjust = -0.3)
head(df4)
ggplot(data = df4, aes(x = day, y = Avg_Casual_Member_Rides)) +
  geom_bar(stat = 'identity', fill = 'Blue') +
  labs(title = 'Each Day Average Rides for Casual Members', x = 'Days', y = 'No.of Rides') +
  geom_text(aes(label = stat(Avg_Casual_Member_Rides)), stat = 'identity', vjust = -0.3)

I have attached the detailed code above, Here the df4 is working fine in the report and produces the graph but df3 is not.

Please do tell me if you need any more clarifications.
Thank you

What is the result of

unique(dframe$Member_Type)

image

What confuses me is that this works perfectly fine while running in the studio(so assuming that the code is correct) but not on the output of markdown report.

ok, please give us

dput(head(dframe,20))

dput(head(dframe,20))

structure(list(X1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20), Ride_ID = c("5DB63F4E4EB6A9CF",
"1FD159E93F7BAFA1", "6D93A270684EC452", "091D47E4F0FC5022", "07F785C9DDA3404C",
"643593E85E46A45C", "782CEA3C6968D2A6", "432C76DCFB84366A", "7912522A5308E3DA",
"545019BF3EF4B419", "5283BF7DA7BC511C", "988C229254E61A66", "6DBE070B761D60A7",
"DE37B4E1E3776DBD", "948FF22F1350EEB4", "4BE6EB51AAC86660", "A425AC8F8D5A6EFE",
"C070E17FD3115E05", "71AFDB68CD4B2F10", "473C341730FEBBAF"),
Rideable_Type = c("docked_bike", "docked_bike", "docked_bike",
"docked_bike", "docked_bike", "docked_bike", "docked_bike",
"docked_bike", "docked_bike", "docked_bike", "docked_bike",
"docked_bike", "docked_bike", "docked_bike", "docked_bike",
"docked_bike", "docked_bike", "docked_bike", "docked_bike",
"docked_bike"), Started_At = c("2020-04-01 0:00:30", "2020-04-01 0:02:35",
"2020-04-01 0:02:41", "2020-04-01 0:06:44", "2020-04-01 0:11:18",
"2020-04-01 0:13:36", "2020-04-01 0:13:41", "2020-04-01 0:21:54",
"2020-04-01 0:23:52", "2020-04-01 0:39:21", "2020-04-01 0:45:39",
"2020-04-01 1:10:49", "2020-04-01 1:39:08", "2020-04-01 2:26:18",
"2020-04-01 2:34:36", "2020-04-01 3:05:34", "2020-04-01 3:38:41",
"2020-04-01 3:41:08", "2020-04-01 3:43:34", "2020-04-01 4:04:20"
), Ended_At = c("2020-04-01 0:23:03", "2020-04-01 0:10:45",
"2020-04-01 0:24:20", "2020-04-01 0:14:01", "2020-04-01 0:11:51",
"2020-04-01 0:18:59", "2020-04-01 0:19:09", "2020-04-01 0:27:20",
"2020-04-01 0:34:42", "2020-04-01 0:43:19", "2020-04-01 0:56:58",
"2020-04-01 1:16:33", "2020-04-01 1:47:41", "2020-04-01 2:57:46",
"2020-04-01 2:47:43", "2020-04-01 3:22:41", "2020-04-01 3:49:17",
"2020-04-01 4:14:22", "2020-04-01 3:52:19", "2020-04-01 4:11:58"
), Day_Of_Week = c("Wednesday", "Wednesday", "Wednesday",
"Wednesday", "Wednesday", "Wednesday", "Wednesday", "Wednesday",
"Wednesday", "Wednesday", "Wednesday", "Wednesday", "Wednesday",
"Wednesday", "Wednesday", "Wednesday", "Wednesday", "Wednesday",
"Wednesday", "Wednesday"), Ride_Length = c(1353, 490, 1299,
437, 33, 323, 328, 326, 650, 238, 679, 344, 513, 1888, 787,
1027, 636, 1994, 525, 458), Start_Station_Name = c("Damen Ave & Wellington Ave",
"Wabash Ave & 16th St", "Damen Ave & Wellington Ave", "Mies van der Rohe Way & Chicago Ave",
"Wabash Ave & 9th St", "Kingsbury St & Erie St", "Kingsbury St & Erie St",
"Sheridan Rd & Irving Park Rd", "California Ave & Altgeld St",
"Blue Island Ave & 18th St", "Clark St & Elm St", "May St & Taylor St",
"Michigan Ave & Lake St", "Michigan Ave & Madison St", "Michigan Ave & Madison St",
"Ogden Ave & Congress Pkwy", "Sheridan Rd & Buena Ave", "Southport Ave & Belmont Ave",
"Emerald Ave & 28th St", "Franklin St & Chicago Ave"), Start_Station_ID = c(162,
72, 162, 173, 321, 74, 74, 240, 502, 129, 176, 22, 52, 197,
197, 122, 306, 154, 207, 31), End_Station_Name = c("Pine Grove Ave & Waveland Ave",
"Wabash Ave & 9th St", "Spaulding Ave & Armitage Ave", "Clark St & Schiller St",
"Wabash Ave & 9th St", "Larrabee St & Division St", "Larrabee St & Division St",
"Pine Grove Ave & Irving Park Rd", "Southport Ave & Clybourn Ave",
"Morgan St & 18th St", "Clinton St & Madison St", "900 W Harrison St",
"Wabash Ave & Roosevelt Rd", "Clark St & Lincoln Ave", "Michigan Ave & Madison St",
"Clark St & Chicago Ave", "Lincoln Ave & Waveland Ave", "Southport Ave & Belmont Ave",
"Emerald Ave & 28th St", "Clark St & Lincoln Ave"), End_Station_ID = c(232,
321, 506, 301, 321, 359, 359, 254, 307, 14, 77, 109, 59,
141, 197, 337, 257, 154, 207, 141), Start_Lattitude = c(41.9359,
41.8604, 41.9359, 41.8969, 41.8708, 41.8938, 41.8938, 41.9542,
41.9267, 41.8576, 41.903, 41.8695, 41.886, 41.8821, 41.8821,
41.875, 41.9585, 41.9395, 41.8436, 41.8967), Start_Longitude = c(-87.6784,
-87.6258, -87.6784, -87.6217, -87.6257, -87.6417, -87.6417,
-87.6544, -87.6977, -87.6615, -87.6313, -87.6555, -87.6241,
-87.6251, -87.6251, -87.6733, -87.655, -87.6638, -87.6454,
-87.6357), End_Lattitude = c(41.9493, 41.8708, 41.9171, 41.908,
41.8708, 41.9035, 41.9035, 41.9544, 41.9208, 41.8581, 41.8822,
41.8748, 41.8672, 41.9157, 41.8821, 41.8968, 41.9488, 41.9395,
41.8436, 41.9157), End_Longitude = c(-87.6463, -87.6257,
-87.7102, -87.6315, -87.6257, -87.6434, -87.6434, -87.648,
-87.6637, -87.6511, -87.6411, -87.6498, -87.626, -87.6346,
-87.6251, -87.6309, -87.6753, -87.6638, -87.6454, -87.6346
), Member_Type = c("casual", "annual", "casual", "annual",
"annual", "annual", "annual", "annual", "annual", "annual",
"annual", "annual", "annual", "casual", "annual", "annual",
"casual", "casual", "annual", "annual")), spec = structure(list(
cols = list(X1 = structure(list(), class = c("collector_double",
"collector")), Ride_ID = structure(list(), class = c("collector_character",
"collector")), Rideable_Type = structure(list(), class = c("collector_character",
"collector")), Started_At = structure(list(), class = c("collector_character",
"collector")), Ended_At = structure(list(), class = c("collector_character",
"collector")), Day_Of_Week = structure(list(), class = c("collector_character",
"collector")), Ride_Length = structure(list(), class = c("collector_double",
"collector")), Start_Station_Name = structure(list(), class = c("collector_character",
"collector")), Start_Station_ID = structure(list(), class = c("collector_double",
"collector")), End_Station_Name = structure(list(), class = c("collector_character",
"collector")), End_Station_ID = structure(list(), class = c("collector_double",
"collector")), Start_Lattitude = structure(list(), class = c("collector_double",
"collector")), Start_Longitude = structure(list(), class = c("collector_double",
"collector")), End_Lattitude = structure(list(), class = c("collector_double",
"collector")), End_Longitude = structure(list(), class = c("collector_double",
"collector")), Member_Type = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"), row.names = c(NA,
20L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"))

Its quite mysterious what is going on, as the data you sent over should at least give you a 3 count for annual average wednesday.

Do you have your code in different places or is it all in the one single rmarkdown that we are debugging ?