logo
down
shadow

Bar colors in ggplot2 geom_bar not applied correctly (R)


Bar colors in ggplot2 geom_bar not applied correctly (R)

By : Lester DeKay
Date : November 22 2020, 02:59 PM
Hope that helps The problem boils down to mapping bar_colors to fill. By default, ggplot2 will assign levels of the fill alphabetically instead of the order they appear in the dataset. When you were assigning the color values in scale_fill_manual, you were essentially assigning the colors in a different order than fill did, and things were mismatched.
If you wanted to go this route (using the colors as your fill variable), you'd want to explicitly make a factor variable and assign the levels in the order they appear in the dataset. I made a separate variable, although you could have replaced the original character variable with the factor and used levels instead of unique in scale_fill_manual.
code :
target_data$bar_colors2 = factor(target_data$bar_colors, levels = unique(target_data$bar_colors))

ggplot(data=target_data, aes(x=UnitCode, y=Minutes, fill=bar_colors2)) + 
    geom_bar(stat="identity") + 
    scale_fill_manual(name="State", 
                   values=unique(target_data$bar_colors),
                   labels = unique(target_data$State))
ggplot(data=target_data, aes(x=UnitCode, y=Minutes, fill=State)) + 
    geom_bar(stat="identity") + 
    scale_fill_manual(values=unique(target_data$bar_colors))


Share : facebook icon twitter icon
ggplot2: Using `fill = …` in aes(..) and geom_bar(..). The colors repeat

ggplot2: Using `fill = …` in aes(..) and geom_bar(..). The colors repeat


By : Jack Wheeler
Date : March 29 2020, 07:55 AM
wish helps you It appears that your data.frame is a summary table. In which case, stat = 'identity' could be appropriate within the geom_bar command. Except not. You need ggplot to perform additional aggregations on the summary table. For the first stacked bar (MORPH_PC1), the components to be stacked are ordered, and, despite stat='identity', ggplot will add the appropriate weights. But if you change the order of the components of the first stacked bar, then it too will contain repeated colours. For instance, use your ggplot command with the following data frame to see the effect. It's your data frame except for a slight change in the order for the Model2 variable.
code :
dat = read.table(text = "      Gene    Morpho Model     Weights Model2
29 3_RH2B Morph_PC1   BM1 0.301666667    BMx
32 3_RH2B Morph_PC1   OUM 0.066666667   OUMx
30 3_RH2B Morph_PC1   BMS 0.478333333    BMx
31 3_RH2B Morph_PC1   OU1 0.091666667    OU1
33 3_RH2B Morph_PC1  OUMA 0.028333333   OUMx
34 3_RH2B Morph_PC1  OUMV 0.023333333   OUMx
35 3_RH2B Morph_PC1 OUMVA 0.008333333   OUMx
36 3_RH2B Morph_PC2   OUM 0.246666667   OUMx
37 3_RH2B Morph_PC2  OUMA 0.171666667   OUMx
38 3_RH2B Morph_PC2  OUMV 0.096666667   OUMx
39 3_RH2B Morph_PC2   BMS 0.106666667    BMx
40 3_RH2B Morph_PC2   OU1 0.213333333    OU1
41 3_RH2B Morph_PC2   BM1 0.140000000    BMx
42 3_RH2B Morph_PC2 OUMVA 0.025000000   OUMx", header = TRUE, sep = "")
datRevised = aggregate(Weights ~ Morpho + Model2, data = dat, FUN = "sum")
ggplot(datRevised, aes(x=Morpho, y=Weights, fill=Model2)) + geom_bar(stat='identity') + ggtitle('RH2B')
ggplot(dat, aes(x=Morpho, weight=Weights, fill=Model2)) + geom_bar() + ggtitle('RH2B')
Using Geom_Bar in GGplot2

Using Geom_Bar in GGplot2


By : user3559151
Date : March 29 2020, 07:55 AM
Hope that helps So I'm trying to use geom_bar in ggplot2, and all of the cases that I see of people demonstrating it online are of comparative frequencies of certain things. The chart that I'm trying to do is the stacked bar graph like this one , I think you can start from this:
code :
v=data.frame(x="My Stacked Bar", y=c(1,2,3,4))

ggplot(data = v, aes(x = factor(x), y = y))+
  geom_bar(aes(fill=factor(y)), stat="identity")
R - ggplot2 geom_bar() doesn't plot correctly column's values

R - ggplot2 geom_bar() doesn't plot correctly column's values


By : André Iván
Date : March 29 2020, 07:55 AM
like below fixes the issue I am new to R
code :
c_count=c(23,45,67,43,54)
country=c("america","india","germany","france","italy")
# sample Data frame #
finaldata = data.frame(country,c_count)
ggplot(finaldata, aes(x=country)) +
geom_bar(aes(weight = c_count))
ggplot2: Using `fill = …` in aes(..) and geom_bar(..). The colors don't repeat

ggplot2: Using `fill = …` in aes(..) and geom_bar(..). The colors don't repeat


By : tsnell
Date : March 29 2020, 07:55 AM
I hope this helps you . Are you expecting something like the following?
(black outline added to each component of the bars for emphasis)
code :
ggplot(df2, 
       aes(x = Chromosome, y = End_position, 
           group = seq(1, nrow(df2)), # or seq(nrow(df2), 1)
           fill = Atr, label = End_position)) +
  geom_bar(stat = "identity", color = "black") +
  geom_text(size = 3, position = position_stack(vjust = 0.5))+
  ggtitle('Stacked chromosome')
Wrong border colors on geom_bar plot in ggplot2

Wrong border colors on geom_bar plot in ggplot2


By : TLW
Date : March 29 2020, 07:55 AM
help you fix your problem Move the colour="black" outside the aes() statement. aes() uses variables from your data to create aesthetics, so you're saying to base color off of the column "black", which doesn't exist.
Also found here
Related Posts Related Posts :
  • How to sort groups within sorted groups?
  • How to get randomForest model output in probability using Caret?
  • how to plot a data frame into R
  • Calculate mean of a range of rows
  • How to sum the values in a text file?
  • Looking up polygons in a shapefile that point belong in... why does it work with some shapefiles, not with others?
  • R creating a list using variable columns from df
  • Determine programmatically if a function call refers to your own function or a package's (or base R's) function?
  • Adding Special Characters to a plot in R
  • How to dynamically change R .libPaths() based on hostname?
  • Print arguments of a function in R
  • Creating Variables via A loop in r
  • How to randomly divide an integer into a fixed number of integers, such that the obtained tuples are uniformly distribut
  • Remove columns using column name based on levels of factors
  • What units are the 'width = ' in geom_bar(aes = ) and position_dodge(width = ) rendered in?
  • Assigning Dates to Fiscal Year
  • conditional removal from data frame
  • Getting variables out of a function in R
  • Create a vector in a dataframe based on matching a second vector to a row name in another object
  • Convert julian day to mm/dd and add year from another column in dataframe in r
  • Change SMOTE parameters inside CARET k-fold cross-validation classification
  • rollapply classes each segment the same
  • rename the header of the spliting group in r
  • Summary stats by factor level for multiple variables
  • Processing Rmarkdown inside chunks
  • R: how to join the duplicate rows in one dataframe
  • Creating a unified time-series, with dates coming from different (natural) languages
  • R error: attempt to apply non-function , randomForest , rfImpute
  • assign multiple color to each vertex in igraph
  • How do I add shading and color to the confidence intervals in ggplot 2 generated Kaplan-Meier plot?
  • How to generate spatial points with a pattern
  • ggplot2 + stat_contour variable binwidth
  • Add fake data to a data frame based on variable condition
  • Utilizing a character vector inside a function
  • match fundction with data frames that are differently constructed
  • Overlaying plots with a horizontal date in R
  • lapply'ing a for loop returns NULL
  • Dynamically add UI elements and gather their input in a dataframe in shiny
  • All the possible outcomes
  • How to Use corrplot with method="number" and Drop Leading Zero?
  • cbind converting factor to numeric
  • create names of loop when iterating data frame
  • R reading multiple raster files gives error in for loop
  • Debugging into and changing R-functions in packages
  • Categorical variables in R - which one does R pick as reference?
  • Create new data frame based on values from another data frame
  • Subplots using Plotly in R (bug fixed)
  • WinBUGS error 'expected key word END' caused by wrong exponential code (not length of data)
  • Predict with gls
  • R list to wide (sparse) data frame
  • Installing ZeligMultilevel. Error : object ‘describe’ is not exported by 'namespace:Zelig'
  • How to use a variable created in a function in your console
  • Dynamic grid plots in shiny R
  • Display unicode in R
  • value of NULL after inserting value
  • Model multiple imputation with interaction terms
  • Dynamically Splitting and naming the dataframe
  • Cross-correlation of 5 time series (distance) and interpretation
  • Grouping columns based on pairwise equality
  • Checking if multiple variables are all the same value in R
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co