ggplot2

Relative frequency histogram in R, ggplot

与世无争的帅哥 提交于 2021-02-08 08:16:08
问题 I can draw relative frequency histogram in R, using lattice package: a <- runif(100) library(lattice) histogram(a) I want to get the same graph in ggplot . I tried dt <- data.frame(a) ggplot(dt, aes(x = a)) + geom_bar(aes(y = ..prop..))+ scale_y_continuous(labels=percent) but it doesn't work like that. What I should change in the code? Calculating relative frequency before graph is not an option for me. 回答1: You want a histogram, not a barplot, so: ggplot(dt, aes(x = a)) + geom_histogram(aes

saving multiple plots in a single PDF in Shiny R

一曲冷凌霜 提交于 2021-02-08 07:41:03
问题 I am trying to export ggplots in my Shiny App into a single PDF file using the download handler but it is not working. The PDF file is getting saved but it gives me only the last ggplot instead of all three. Any help would be appreciated! Below is the code of the server: shinyServer(function(input, output, session) { plotinput() { df<-data.frame(q=c(1,3,5,7,9),w=c(2,4,6,8,10),z=c(1,2,3,4,5)) ggplot(df,aes(x=q,y=w))+geom_point() ggplot(df,aes(x=z,y=w))+geom_point() ggplot(df,aes(x=q,y=z))+geom

Problems adapting the y-axis to 2x2 ANOVA bargraph using R and ggplot

早过忘川 提交于 2021-02-08 07:40:29
问题 I am not a Pro R user but I already tried multiple things and can't find a solution to the problem. I created a bar graph for 2x2 ANOVA including error bars, APA theme and custom colors based on this website: https://sakaluk.wordpress.com/2015/08/27/6-make-it-pretty-plotting-2-way-interactions-with-ggplot2/ It works nicely but the y-axis starts at 0 although my scale only ranges from 1 - 7. I am trying to adapt the axis but I get strange errors. This is what I did: # see https://sakaluk

saving multiple plots in a single PDF in Shiny R

老子叫甜甜 提交于 2021-02-08 07:39:55
问题 I am trying to export ggplots in my Shiny App into a single PDF file using the download handler but it is not working. The PDF file is getting saved but it gives me only the last ggplot instead of all three. Any help would be appreciated! Below is the code of the server: shinyServer(function(input, output, session) { plotinput() { df<-data.frame(q=c(1,3,5,7,9),w=c(2,4,6,8,10),z=c(1,2,3,4,5)) ggplot(df,aes(x=q,y=w))+geom_point() ggplot(df,aes(x=z,y=w))+geom_point() ggplot(df,aes(x=q,y=z))+geom

How to correctly order segments by value, within an individual bar, on a bar chart in ggplot (even when changing from a factor)

北城以北 提交于 2021-02-08 07:39:38
问题 I am trying in vain to get the segments within each bar of the bar chart to order based on the value (largest value within a bar on the bottom, smallest on top). I've researched this and would think this should work, but something is not right and I can't find the issue. I tried this solution here, but no luck. Here is a reproducible example: library(dplyr) my_repro <- structure(list(Date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "2020-04-01", class =

How to format date on x-axis to month and year in R

亡梦爱人 提交于 2021-02-08 06:43:27
问题 I've seen several questions posted about formatting the date and haven't been able to find one to resolve the issue I'm having here. My data ranges from May-November from 2008-2015 and then drops May and goes from June-November from 2016-2018. My goal is to create a plot to show the months sampled for each year and eliminate the months that were not sampled. I asked a similar question here: How to plot mean density by year and month But this answer does not resolve the date issue. I've tried

How to find the intersecting point in the regression line

余生长醉 提交于 2021-02-08 05:44:22
问题 In the above graph two vertical and horizontal line segments are drawn which will intersect the regression line. How to write code to draw those lines to find the intersecting point? 回答1: The formula for each regression line is printed on the plot, so we can get the information by simple algebra. First we will plot each regression line: x <- c(0, 22) y0 <- 27.46 + 0.31 * x y5 <- 40.18 + 0.49 * x y10 <- 55.54 + 0.67 * x y15 <- 71.63 + 0.84 * x plot(x, y0, type = "l", ylim = c(0, 105), xlim = c

Generate boxplots for multiple variables in ggplot2 without factoring [duplicate]

不问归期 提交于 2021-02-08 05:42:07
问题 This question already has answers here : Building a box plot from all columns of data frame with column names on x in ggplot2 [duplicate] (1 answer) Multiple boxplots using ggplot (1 answer) Closed 3 years ago . EDIT: Added the boxplot generated with standard boxplot() function. Given the iris dataste, the following code: boxplot(iris[,]) Creates a boxplot with five boxes, one for each variable, without splitting them into categories such as, for instance, species. While this is simple enough

Generate boxplots for multiple variables in ggplot2 without factoring [duplicate]

。_饼干妹妹 提交于 2021-02-08 05:41:28
问题 This question already has answers here : Building a box plot from all columns of data frame with column names on x in ggplot2 [duplicate] (1 answer) Multiple boxplots using ggplot (1 answer) Closed 3 years ago . EDIT: Added the boxplot generated with standard boxplot() function. Given the iris dataste, the following code: boxplot(iris[,]) Creates a boxplot with five boxes, one for each variable, without splitting them into categories such as, for instance, species. While this is simple enough

Adding percentage labels to ggplot when using stat_count

一个人想着一个人 提交于 2021-02-08 05:16:30
问题 For some reason, I can't seem to be able to add correct proportion labels to a ggplot by using stat_count . The code below returns labels that display 100% for all categories, even though I'm using ..prop.. . Should I use something else instead of stat_count ? library(tidyverse) diamonds %>% ggplot(aes(color, fill=cut)) + geom_bar(position = 'fill') + stat_count(aes(label= scales::percent(..prop..)), geom = 'text', position = position_fill(vjust = 0.5)) I know this can also be accomplished by