logo
down
shadow

Grouped barplot with R


Grouped barplot with R

By : Sherrie Campbell
Date : December 01 2020, 05:00 PM
hop of those help? I am trying to draw a grouped bar plot with the following data , This would solve your problem.
code :
     library(reshape2)
     library(ggplot2)
     df.long<-melt(data)
     df.long$groups <- factor(df.long$groups, levels=unique(df.long$groups))
     ggplot(df.long,aes(x=groups,y=value,fill=variable))+ labs(x = "groups", y = "connections") + geom_bar(stat="identity",position="dodge")


Share : facebook icon twitter icon
Grouped bar Graph using barplot

Grouped bar Graph using barplot


By : Jan
Date : March 29 2020, 07:55 AM
Hope this helps First, do not double post! If necessary edit your previous question, rather than posting a new question.
Second, the error message that you mentioned in your earlier post is pretty self-explanatory. Here's the error message:
code :
mymat <- t(mydf[-1])
colnames(mymat) <- mydf[, 1]
barplot(mymat, beside = TRUE)
Grouped barplot with cut y axis

Grouped barplot with cut y axis


By : Manju
Date : March 29 2020, 07:55 AM
it helps some times You could do this manually. Like barplot, ?gap.barplot returns the center positions of the bars. Use these to add the labels.
Using space for spacing between groups as in regular barplot does not seem to work. We can use a row of NAs to hack a space.
code :
d = t(matrix( c(7,3,2,3,2,2,852,268,128,150,
                                127,74,5140,1681,860,963,866,
                                470,26419,8795,4521,5375,4514,2487),
                            nrow=6, ncol=4 ))
colnames(d)=c("A", "B", "C", "D", "E", "F")

# add row of NAs for spacing
d=rbind(NA,d)

# install.packages('plotrix', dependencies = TRUE)
require(plotrix)

# create barplot and store returned value in 'a'
a = gap.barplot(as.matrix(d), 
                gap=c(9600,23400), 
                ytics=c(0,3000,6000,9000,24000,25200,26400),
                xaxt='n') # disable the default x-axis

# calculate mean x-position for each group, omitting the first row 
# first row (NAs) is only there for spacing between groups
aa = matrix(a, nrow=nrow(d))
xticks = colMeans(aa[2:nrow(d),])

# add axis labels at mean position
axis(1, at=xticks, lab=LETTERS[1:6])
How can I do a grouped barplot?

How can I do a grouped barplot?


By : user3274755
Date : March 29 2020, 07:55 AM
should help you out ggplot needs an object of type data.frame, not a matrix or table. You can create it this way:
code :
 identification <- data.frame(Party=c("CDU/CSU", "CDU/CSU", ...),
                              Type=c("Neg", "Pos", ...),
                              Resultat=c(meanNegCDUCSU, meanPosCDUCSU, ...))
Grouped scatterplot over grouped barplot

Grouped scatterplot over grouped barplot


By : user3473759
Date : March 29 2020, 07:55 AM
seems to work fine The problem you are having, I think, is that some geometries, such as geom_point and geom_jitter do not use or allow the factor aesthetic for grouping along the x-axis.
Thus when you plot other geoms on top of a chart where factor is not ignored, such as geom_bar, the factor setting is ignored for some layers but not for bar, and you don't get year-resolved columns of points.
code :
require(Ecdat)
data(Males)
quartz(height=6, width=12)

ggplot(Males, aes(x=year, y=wage)) +
  facet_grid(.~ethn) +
  stat_summary(mapping=aes(fill=year), fun.y=mean, geom='bar') +
  stat_summary(fun.data = mean_sdl, geom='errorbar', color='yellow') +
  geom_jitter(aes(color=industry), 
              position=position_jitter(width=0.2), alpha=0.8)

quartz.save('SO_29610340.png')
How to create grouped barplot from dataframe grouped by two columns with intervals

How to create grouped barplot from dataframe grouped by two columns with intervals


By : BeardedSloth
Date : March 29 2020, 07:55 AM
I hope this helps you . I grouped the data by two columns ('a' and 'b'). And for each column, I cut it into bins. For each crossed bin, I would like to have the sum of 'c'. Then I want to create a grouped plot for the sum of 'c'. However, when I use .plot(kind='bar') after the grouped data, the bars were not grouped together. , Given the following example data
code :
df = pd.DataFrame({'a': [8, 49, 81, 52, 22, 24, 32, 67, 24, 21, 78, 16, 86, 19, 4, 88, 4, 20, 20, 1],
                   'b': [2, 49, 49, 70, 31, 47, 98, 26, 55, 36, 17, 39, 56, 80, 52, 36, 42, 69, 73, 70],
                   'c': [22, 99, 31, 95, 16, 32, 81, 20, 58, 23, 53, 5, 0, 81, 8, 68, 16, 36, 35, 54]})
grouped = df.groupby([pd.cut(df['a'], [16, 30, 67, 86]),
                      pd.cut(df['b'], [0, 26, 49, 100])])
grouped = grouped['c'].sum()
(grouped.reset_index()
        .pivot('a', 'b')
        .plot(kind='bar'));
grouped = grouped.unstack()
grouped.columns = grouped.columns.categories
grouped.plot(kind='bar');
Related Posts Related Posts :
  • Fill in missing data that is same across columns
  • Remove same columns from left_join
  • Strange addTaskCallback work in RStudio
  • Make dual X-axs based on different variables using ggvis
  • Generate a normal distribution within certain limits in R
  • from data table, randomly select one row per group
  • ngrams not in correct order
  • ggplot: plot title and plot overlap each other
  • Display groups with different borders in histogram with panel.superpose
  • Submit form with no submit button in rvest
  • Can't download .csv from dropbox
  • How to get Adjusted R-Square value as variable?
  • Check row-wise if element exists in comma-separated column with position
  • How to know which nodes in a graph are removable
  • Generate all possible permutations of a binary matrix
  • R, Windows and foreign language characters
  • Datapusher with missing values and mixed (srting/numeric) data
  • Adding greek characters with variables to axis title
  • R - Subsetting subsets of variable names in loops
  • R dynamically naming a data frame to be used in ggplot2
  • Optimize/vectorize a loop in R that generates randoms from ranges in input vectors?
  • 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
  • Bar colors in ggplot2 geom_bar not applied correctly (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
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co