geom-bar

order and fill with 2 different variables geom_bar ggplot2 R

こ雲淡風輕ζ 提交于 2019-12-09 17:59:00
问题 I have a question concerning the fill field in geom_bar of the ggplot2 package. I would like to fill my geom_bar with a variable (in the next example the variable is called var_fill ) but order the geom_plot with another variable (called clarity in the example). How can I do that? Thank you very much! The example: rm(list=ls()) set.seed(1) library(dplyr) data_ex <- diamonds %>% group_by(cut, clarity) %>% summarise(count = n()) %>% ungroup() %>% mutate(var_fill= LETTERS[sample.int(3, 40,

Control bar border (color) thickness with ggplot2 stroke

。_饼干妹妹 提交于 2019-12-09 05:21:45
问题 Is it possible to use the stroke argument introduced with ggplot2 2.0 to adjust the thickness of borders around bars? If not, is there a way to control bar-border thickness along the lines of point-border thickness? Stroke applies to borders around certain shapes -- see the second answer A very modest MWE, showing fill only: factor <- c("One", "Two", "Three", "Four") value <- c(1, 2, 3, 4) factor2 <- c("A", "B", "A", "B") df <- data.frame(factor = factor(factor, levels = factor), value =

Label error in geom_bar [duplicate]

笑着哭i 提交于 2019-12-08 13:58:03
问题 This question already has answers here : Showing data values on stacked bar chart in ggplot2 (2 answers) Closed 5 years ago . I'm trying put label in geom_bar but I can't. My code df <- data.frame( uni = rep(c("D","E","F","G","H"),3), Var1 = factor(c(rep("A",5),rep("B",5),rep("C",5))), Freq = c(53.6,50.0,48.5,50.0,56.2,23.2,18.5,27.7,20.0,14.3,23.2,31.5,23.8,30.0,29.6)) df$label = paste(round(df$Freq,0),"%", sep = "") ggplot(data = df, aes(x = uni, y = Freq, fill = Var1)) + geom_bar(stat =

ggplot2 How to create vertical line corresponding to quantile in geom_bar plot

三世轮回 提交于 2019-12-07 18:45:06
问题 Currently, I can create a plot such as this: geom_bar ggplot(df.Acc, aes(x = reorder(cities, -accidents), y = accidents)) + geom_bar(stat = "identity", fill="steelblue", alpha=0.75) + geom_hline(yintercept=0, size=0.4, color="black") It's a plot with, let's say, number of bike accidents per year on the y-axis, and the city name would be on the x-axis. I want to add a vertical line to separate all the cities above the 70th percentile and below it. So I've tried with > vlinAcc <- quantile(df

geom_bar from min to max data value

做~自己de王妃 提交于 2019-12-06 19:19:26
问题 I am not sure if geom_bar is able (probably I'm not) to create the plot I need with geom_bar . I want a bar plot but not starting from 0, I have a minimum and maximum value that are to be the start and end points of each bar. Desired output looks like this one: Data structure is dput(datos) structure(list(CDG = c("Alicante", "Alicante", "Alicante", "Alicante", "Alicante", "Alicante", "Alicante", "Alicante", "Alicante", "Alicante", "Alicante", "Castellón", "Castellón", "Castellón", "Castellón"

ggplot2: create ordered group bar plot - (use reorder)

我只是一个虾纸丫 提交于 2019-12-06 15:18:57
问题 I want to create grouped bar plot while keeping order. If it was single column and not a grouped bar plot use of reorder function is obvious. But not sure how to use it on a melted data.frame. Here is the detail explanation with code example: Lets say we have following data.frame: d.nfl <- data.frame(Team1=c("Vikings", "Chicago", "GreenBay", "Detroit"), Win=c(20, 13, 9, 12)) plotting a simple bar plot while flipping it. ggplot(d.nfl, aes(x = Team1, y=Win)) + geom_bar(aes(fill=Team1), stat=

Grouped bar chart on R using ggplot2

泄露秘密 提交于 2019-12-06 13:15:11
问题 How do I create a grouped bar chart on R using ggplot2 using this data? Person Cats Dogs Mr. A 3 1 Mr. B 4 2 So that it shows that shows number of pets owned per person, with this layout Bar chart of pets I have a text file with this data and have used read.delim to read the file on R. I have used this code but it does not produce the bar plot I am looking for. ggplot(data=pets, aes(x=Person, y=Cats, fill=Dogs)) + geom_bar(stat="identity", position=position_dodge()) I am new to R, any help

ggplot2: geom_bar stacked barplot, specify bar outline color

不打扰是莪最后的温柔 提交于 2019-12-06 08:20:30
问题 I am trying to figure out how to specify the outline color on a stacked barplot in ggplot2. In the below code I specify color="green" , which gives a green outline to each of the bars. I would like to specify a different outline color for each bar (e.g. cut=Fair would be filled with yellow and outlined with orange, cut=Good would be filled with light green and outlined with dark green, etc.). ggplot(diamonds) + geom_bar(aes(clarity, fill=cut))+ scale_fill_manual(values=c("Fair"="yellow","Good

Control the fill order and groups for a ggplot2 geom_bar

感情迁移 提交于 2019-12-06 07:08:30
问题 library(ggplot2) data <- data.frame( group=factor(c("a","c","b","b","c","a")), x=c("A","B","C", "D","E","F"), y=c(3,2,10,11,4,5)) > data group x y 1 a A 3 2 c B 2 3 b C 10 4 b D 11 5 c E 4 6 a F 5 #And plot this: ggplot(data)+ geom_bar(aes(x=x, y=y, fill=group, order=group), stat="identity", position="dodge")+ coord_flip() This gives a figure where x is plotted according to factor levels: But how can one reorder x according to a custom order of the group variable and at the same time arrange

ggplot2: Add label on barplot if position = “fill”

偶尔善良 提交于 2019-12-06 04:51:26
I would like to add %-figures on a filled barplot. Here is the plot with the labels at the wrong places: Here is the dataframe: x0 <- expand.grid(grp = c("G1","G2") , treat = c("T1","T2") , out = c("out1","out2","out3","out4") ) set.seed(1234) x0$n <- round(runif(16,0,1)*100,0) head(x0) grp treat out n 1 G1 T1 out1 11 2 G2 T1 out1 62 3 G1 T2 out1 61 4 G2 T2 out1 62 5 G1 T1 out2 86 6 G2 T1 out2 64 Now, I add the sum within grp/treat to the dataframe (using sql, sorry!): x0 <- sqldf(paste("SELECT a.*, (SELECT SUM(n)" ," FROM x0 b" ," WHERE a.grp = b.grp" ," AND a.treat = b.treat" ," ) tot" ,"