ggplot2

How can I solve non-finite location and/or size for viewport error?

偶尔善良 提交于 2021-02-17 04:52:05
问题 Here is a simplified and testable example: dataset <- data.frame( emp_month = c("January","March","April","May","December"), salary = c(623.3,515.2,611.0,729.0,843.25)) library(ggplot2) ggplot(dataset)+ geom_boxplot(aes(x = sort(factor(emp_month)), y = salary))+ geom_point(aes( x = sort(factor(emp_month)), y=salary))+ facet_grid(. ~ sort(factor(emp_month)),space = "free", scales="free",margins = T) Error Description: I can write this code library(ggplot2) MesDeConclusao = factor

Create ggplot2 legend for multiple datasets

寵の児 提交于 2021-02-16 14:55:09
问题 I am trying to display background data in grey in a ggplot with legend automatically. My aim is to either include the grey datapoints in the legend, or to make a second legend with a manual title. However I fail at doing any of the two. My data is in long format. require(ggplot2) xx<-data.frame(observation="all cats",x=1:2,y=1:2) yy<-data.frame(observation=c("red cats","blue cats"),x=3:4,y=3:4) g<-ggplot() + geom_point(aes(x,y, colour=factor(observation)), colour="grey60", size=5, data=xx) +

How to avoid the crossing effect in legend with geom_vline() and geom_hline on the same scatter plot?

谁都会走 提交于 2021-02-16 14:26:52
问题 I created a scatter plot with geom_hline() and geom_vline() , the plot is good but the legend entries are not how I would like to make them appear. The vline (Restauration) and hline (Threshold) are crossing each other in the legend, making it confusing. I want the restauration legend entry to be an orange vertical line and the Threshold legend entry to be a horizontal black line. I tried several things suggested in other posts, with guide_legend(override.aes()) or with show.legend = F but

Plotting a list of timeseries of class(forecast) in [R]

拥有回忆 提交于 2021-02-16 13:47:20
问题 I am trying to plot a faceted grid of timeseries plots (ideally 3X3) using a list of forecast timeseries data. The data is nested within a list and is of class forecast::forecast. > class(forecasts) [1] "list" > class(forecasts$`1_1`) [1] "forecast" > head(forecasts, 2) $`1_1` Point Forecast Lo 80 Hi 80 Lo 95 Hi 95 Dec 2016 7.370299 7.335176 7.405422 7.316583 7.424015 $`1_10` Point Forecast Lo 80 Hi 80 Lo 95 Hi 95 Dec 2016 7.396656 7.359845 7.433467 7.340359 7.452953 I would like to plot the

How do I use color in a geom_dotplot?

天涯浪子 提交于 2021-02-16 13:30:39
问题 I have this dotplot: ggplot(mpg, aes(drv, hwy)) + geom_dotplot(binwidth = 1, binaxis = 'y', stackdir = 'center') which renders as I want to color the dots by manufacturer. If I add a fill aesthetic: ggplot(mpg, aes(drv, hwy, fill = manufacturer)) + geom_dotplot(binwidth = 1, binaxis = 'y', stackdir = 'center') it renders as It looks like adding color has defeated the stacking algorithm somehow, causing the dots to overlap. How do I fix this? 回答1: This is the closest I've gotten: ggplot(mpg,

How do I use color in a geom_dotplot?

给你一囗甜甜゛ 提交于 2021-02-16 13:30:08
问题 I have this dotplot: ggplot(mpg, aes(drv, hwy)) + geom_dotplot(binwidth = 1, binaxis = 'y', stackdir = 'center') which renders as I want to color the dots by manufacturer. If I add a fill aesthetic: ggplot(mpg, aes(drv, hwy, fill = manufacturer)) + geom_dotplot(binwidth = 1, binaxis = 'y', stackdir = 'center') it renders as It looks like adding color has defeated the stacking algorithm somehow, causing the dots to overlap. How do I fix this? 回答1: This is the closest I've gotten: ggplot(mpg,

scale_fill_manual is not working in geom_bar

让人想犯罪 __ 提交于 2021-02-16 13:15:14
问题 I'm trying to change the color in a barplot in ggplot2 using scale_fill_manual, but for some reason only works if i use the fill option inside aesthetics. I made an example: library(ggplot2) library(dplyr) iris %>% ggplot(aes(x=Sepal.Width,y=Sepal.Length))+ geom_bar(stat="identity") + scale_fill_manual(values='lightblue') Here is the result, no changing in the color: Now, using the fill option inside aesthetics, it works: iris %>% ggplot(aes(x=Sepal.Width,y=Sepal.Length, fill=factor(2) ))+

scale_fill_manual is not working in geom_bar

柔情痞子 提交于 2021-02-16 13:15:09
问题 I'm trying to change the color in a barplot in ggplot2 using scale_fill_manual, but for some reason only works if i use the fill option inside aesthetics. I made an example: library(ggplot2) library(dplyr) iris %>% ggplot(aes(x=Sepal.Width,y=Sepal.Length))+ geom_bar(stat="identity") + scale_fill_manual(values='lightblue') Here is the result, no changing in the color: Now, using the fill option inside aesthetics, it works: iris %>% ggplot(aes(x=Sepal.Width,y=Sepal.Length, fill=factor(2) ))+

Create a bin for anything above X value in GGPlot2 Histogram

99封情书 提交于 2021-02-16 10:22:12
问题 Using ggplot2 , I want to create a histogram where anything above X is grouped into the final bin. For example, if most of my distribution was between 100 and 200, and I wanted to bin by 10, I would want anything above 200 to be binned in "200+". # create some fake data id <- sample(1:100000, 10000, rep=T) visits <- sample(1:1200,10000, rep=T) #merge to create a dataframe df <- data.frame(cbind(id,visits)) #plot the data hist <- ggplot(df, aes(x=visits)) + geom_histogram(binwidth=50) How can

How to reverse point size in ggplot?

巧了我就是萌 提交于 2021-02-15 19:21:06
问题 Please, help me with this point. I need the positive values to be represented as small points and negative as large points. If I tape minus before size, the point sizes are right but the legend is changing: df=data.frame(x=rnorm(20),y=runif(20),z=rnorm(20)) ggplot(df,aes(x=x,y=y))+geom_point(aes(size=-z)) so that does not suite. 回答1: One solution would be to use scale_size() and set your own breaks and then labels in opposite direction. Changed range of z values to get better representation.