ggplot2

How to create a stacked bar chart with 2 numeric variables in R using ggplot, grouped by 1 factor variable?

浪尽此生 提交于 2021-02-11 12:47:04
问题 I'm fairly new to R and this is my first question on here so apologies if I write it in a confusing way! Basically I have a dataframe with 4 columns, month and site which are factors, and organic % and inorganic % which are numeric: df<-data.frame(month = c("Mar", "Apr", "May", "Jun", "Jul", "Mar", "Apr", "May", "Jun", "Jul"), site = c("Borth", "Borth", "Borth", "Borth", "Borth", "Clarach", "Clarach", "Clarach", "Clarach", "Clarach"), organic = c(10,20,30,40,50), inorganic = c(90,80,70,60,50)

How to create a stacked bar chart with 2 numeric variables in R using ggplot, grouped by 1 factor variable?

烈酒焚心 提交于 2021-02-11 12:46:54
问题 I'm fairly new to R and this is my first question on here so apologies if I write it in a confusing way! Basically I have a dataframe with 4 columns, month and site which are factors, and organic % and inorganic % which are numeric: df<-data.frame(month = c("Mar", "Apr", "May", "Jun", "Jul", "Mar", "Apr", "May", "Jun", "Jul"), site = c("Borth", "Borth", "Borth", "Borth", "Borth", "Clarach", "Clarach", "Clarach", "Clarach", "Clarach"), organic = c(10,20,30,40,50), inorganic = c(90,80,70,60,50)

How can I change the legend geometry in ggplot2

霸气de小男生 提交于 2021-02-11 11:50:36
问题 Hello say I plot this boxplot: library(ggplot2) DT <- data.frame( y = runif(400, max = 2), grp = sample(c('M', 'F'),size = 400, replace = T), x = rep(as.Date(1:10,origin='2011-01-01'), each = 40) ) p <- ggplot(DT) + geom_boxplot() + aes(x = x, y = y, group=interaction(x,grp), fill=grp) p Question is how can I replace those little boxes in the legend by lines (like I would have using graphics ) 回答1: easiest option might be to make the lines invisible, p + guides(fill = guide_legend(override

Move labels in ggplot2 pie graph

守給你的承諾、 提交于 2021-02-11 10:33:48
问题 I know from How to avoid label overlap in pie chart that I can use ggrepel to make labels not overlap in a pie graph. I would like percentages less than 7% moved to the outside and numbers 7% or more on top of their slice of the pie. Any ideas? library( ggrepel ) library( ggplot2) library( dplyr) library( scales ) library( reshape ) y <- data.frame( state = c( "AR" ) , ac = c( 0.43 ) , man = c( 0.26 ) , ltc = c( 0.25 ) , care = c( 0.05 ) , dsh = c( 0.01 ) ) y2 <- melt( y , id.var="state" )

Move labels in ggplot2 pie graph

怎甘沉沦 提交于 2021-02-11 10:31:27
问题 I know from How to avoid label overlap in pie chart that I can use ggrepel to make labels not overlap in a pie graph. I would like percentages less than 7% moved to the outside and numbers 7% or more on top of their slice of the pie. Any ideas? library( ggrepel ) library( ggplot2) library( dplyr) library( scales ) library( reshape ) y <- data.frame( state = c( "AR" ) , ac = c( 0.43 ) , man = c( 0.26 ) , ltc = c( 0.25 ) , care = c( 0.05 ) , dsh = c( 0.01 ) ) y2 <- melt( y , id.var="state" )

Can't get geom_sf line width to function properly when I provide its value through an aesthetic

旧时模样 提交于 2021-02-11 09:45:19
问题 I have a shapefile, my_sf . If I write: ggplot() + geom_sf( data = my_sf, size = 0.5 # this is the default value actually ) then it works just fine. However, if I add a my_line_width attribute to my_sf , and set each value in that column to 0.5, and then write: ggplot() + geom_sf( data = my_sf, aes(size = my_line_width) ) then I get a bunch of massive lines that overlap hideously. If the normal lines are width 0.5, then these are perhaps width 4. I don't understand why those don't produce the

Can't get geom_sf line width to function properly when I provide its value through an aesthetic

半腔热情 提交于 2021-02-11 09:44:15
问题 I have a shapefile, my_sf . If I write: ggplot() + geom_sf( data = my_sf, size = 0.5 # this is the default value actually ) then it works just fine. However, if I add a my_line_width attribute to my_sf , and set each value in that column to 0.5, and then write: ggplot() + geom_sf( data = my_sf, aes(size = my_line_width) ) then I get a bunch of massive lines that overlap hideously. If the normal lines are width 0.5, then these are perhaps width 4. I don't understand why those don't produce the

R: Error in FUN(X[[i]], …) : only defined on a data frame with all numeric variables

邮差的信 提交于 2021-02-11 08:22:08
问题 I am working with the R programming language. I am trying to plot some categorical and continuous data that I am working with, but I am getting an error that tells me that such plots are only possible with "only numeric variables". library(survival) library(ggplot2) data(lung) data = lung data$sex = as.factor(data$sex) data$status = as.factor(data$status) data$ph.ecog = as.factor(data$ph.ecog) str(data) #plot mycolours <- rainbow(length(unique(data$sex)), end = 0.6) # png("gally.png", 500,

R: Error in FUN(X[[i]], …) : only defined on a data frame with all numeric variables

微笑、不失礼 提交于 2021-02-11 08:19:42
问题 I am working with the R programming language. I am trying to plot some categorical and continuous data that I am working with, but I am getting an error that tells me that such plots are only possible with "only numeric variables". library(survival) library(ggplot2) data(lung) data = lung data$sex = as.factor(data$sex) data$status = as.factor(data$status) data$ph.ecog = as.factor(data$ph.ecog) str(data) #plot mycolours <- rainbow(length(unique(data$sex)), end = 0.6) # png("gally.png", 500,

Changing lower limit of graph y-axis when x is a discrete variable without cutting off bottom of bars

本秂侑毒 提交于 2021-02-11 06:52:08
问题 I am trying to adjust the y-axis of this graph so that it starts at 1 instead of 0. What's the best way to do this? The solution offered here cuts off the bottom of the graph. I would like for the bars to look pretty much identical to the graph below, but with the lower y-limit at 1 and each bar moved down 1 unit to match. I would like to preserve the small amount of gray space below each bar. Code: groups %>% ungroup() %>% mutate(message = fct_relevel(message, "Personal", "General"),