logo
down
shadow

ggplot2() plotting one variable against itself by factor?


ggplot2() plotting one variable against itself by factor?

Content Index :

ggplot2() plotting one variable against itself by factor?
Tag : r , By : beng
Date : November 25 2020, 04:01 AM

I think the issue was by ths following , I'm trying to use ggplot to create a geom_point with two lines, typically these two lines are from two different variables within a dataframe e.g. , Using tidyverse functions, you can do
library(dplyr)
library(tidyr)
library(ggplot2)
dd %>% 
  group_by(fac) %>% 
  mutate(id=1:n()) %>% 
  spread(fac, score) %>% 
  ggplot(aes(trial2, trial1)) + 
  geom_line()

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Plotting non-overlapping levels of a factor on a circular plot using ggplot2, R


Tag : r , By : Brian Drum
Date : March 29 2020, 07:55 AM
it helps some times I am trying to create a circular plot to the display frequency/counts of months in my dataset but I would also like to group the months by season. Here is a similar plot for time of day, and now I would like to use the same approach to plot months/seasons. However, for some reason I can't seem to specify the right option to break my scale into non-overlapping month categories. Any suggestions are much appreciated. , Following simple version works:
ggplot(eventdata, aes(x = factor(months), fill = season)) + 
  geom_histogram()+
  coord_polar()

geom_bar ggplot2 plotting category but assigning colour scales based on an additional factor


Tag : r , By : jaime
Date : March 29 2020, 07:55 AM
hop of those help? Here's a solution as per the comment thread, using facets for type and colours for species. It may be less effective than this example in practice, depending on how many dates, types and species there are in the data.
ggplot(df, aes(date, count)) + 
  geom_col(aes(fill = species)) + 
  facet_grid(. ~ type) + 
  scale_fill_brewer(palette = "Spectral") +
  theme_light()

Reordering factor for plotting using forcats and ggplot2 packages from tidyverse


Tag : r , By : user121350
Date : March 29 2020, 07:55 AM
it helps some times First of all, thanks^13 to tidyverse. I want the bars in the chart below to follow the same factor levels reordered by forcats::fct_reorder (). Surprisingly, I see different order of levels in the data set when View ()ed as when they are displayed in the chart (see below). The chart should illustrate the number of failed students before and after the bonus marks (I want to sort the bars based on the number of failed students before the bonus). , Use factor with unique levels for your x -axis.
ggplot (df) + 
 geom_bar (aes(factor(forcats::fct_reorder
 (subject, FailNo, .desc= TRUE), 
 levels=unique(subject)), 
 FailNo, 
 fill = forcats::fct_rev (Bonus)), 
 position = 'dodge', stat = 'identity') + 
 theme(axis.text.x=element_text(angle=45, vjust=1.5, hjust=1.5, size = rel (1.2)))

R - ggplot2 - plotting one factor by two factors


Tag : r , By : Munir
Date : March 29 2020, 07:55 AM
To fix the issue you can do I'm following up on this link: ggplot2 bar plot with two categorical variables , We could use facet_wrap
library(tidyverse)
df %>%
   group_by_at(names(.)) %>%
   summarise(n = n()) %>%
   ggplot(., aes(x = Fruit, y = n, fill = Bug)) + 
   geom_bar(stat = "identity") +
   facet_wrap(~ group)
Fruit <- c(rep("Apple",3),rep("Orange",5))
Bug <- c("worm","spider","spider","worm","worm","worm","worm","spider")
group <- rep(LETTERS[1:2], each = 4)
df <- data.frame(Fruit,Bug, group)

Why does ggplot2 ignore factor level order when plotting in this script?


Tag : r , By : socurious
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , For reasons that I don't really understand, the factor order is ignored when you use geom_point twice. Modifying the data so that you only need to call geom_point once fixed the problem.
library(tidyverse)

df <- data.frame(
  type = as.factor(c("Chain", "PTM", "PTM", "Motif", "Motif", "PTM", "Motif", "Chain", "PTM", "PTM", "Motif", "Motif")),
  description = as.factor(c("seq", "methyl", "methyl", "RXL", "RXL", "amine", "CXXC", "seq", "amine", "methyl", "CXXC", "RXL")),
  begin = c(1, 20, 75, 150, 67, 289, 100, 1, 124, 89, 73, 6),
  order = c(1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2),
  length = c(300, 1, 1, 1, 1, 1, 1, 350, 1, 1, 1, 1)
)

#set desired order of factor levels
df <- df %>% mutate(
  order = if_else(type == "PTM", true = order + 0.25, false = order),
  description = factor(description, levels = c("amine", "methyl", "RXL", "seq", "CXXC")))

plot_start <- -100
plot_end <- 500

dfplot <- ggplot() +
  xlim(plot_start, plot_end) +
  scale_y_continuous(expand = c(0,0), limits =c(0, 2.5))

# white background
dfplot <- dfplot + theme_bw() +  
  theme(panel.grid.minor=element_blank(),
        panel.grid.major=element_blank()) +
  theme(axis.ticks = element_blank(),
        axis.text.y = element_blank()) +
  theme(panel.border = element_blank())

#plot chains
dfplot <- dfplot + geom_rect(data= df[df$type == "Chain",],
                             mapping=aes(xmin=begin,
                                         xmax=length,
                                         ymin=order-0.2,
                                         ymax=order+0.2),
                             colour = "blue",
                             fill = "#C4D9E9")



#plot motif positions

dfplot <- dfplot + geom_point(data = filter(df, type %in% c("PTM", "Motif")),
                              aes(begin, order, shape = description, color = description), 
                              size = 3)
dfplot
Related Posts Related QUESTIONS :
  • Sum rows values one after the other
  • Nesting ifelse inside summarytools
  • How best to divide different levels of a factor by one another in dataframe in R?
  • Why does my code run multiple times before I type data into the table? How do I make an action button that creates a tab
  • How to impute missing values not at random?
  • Set the y limits of an added average line of a plotly plot
  • how to calculate a new column after grouping with dplyr
  • Extract data from rows creating new columns using R
  • Create a filled area line plot with plotly
  • When do I need parentheses around an if statement to control the sequence of a formula in R?
  • my graph in ggplot2 contains an "e" character in y-axis
  • Making variables immutable in R
  • R: Difference between the subsequent ranks of a item group by date
  • Match data within multiple time-frames with dplyr
  • Conditional manipulation and extension of rows in data.table also considering previous extensions without for-loop
  • Conditional formula referring to preview row in DF not working
  • Set hoverinfo text in plotly scatterplot
  • Histogram of Sums from Categorical/Binary Data
  • Efficiently find set differences and generate random sample
  • Find closest points from data set B to point in data set A, using lat long in R
  • dplyr join on column A OR column B
  • Replace all string if row starts with (within a column)
  • Is there a possibility to combine position_stack and nudge_x in a stacked bar chart in ggplot2?
  • How can I extract bounding boxes in a row-wise manner using R?
  • How do I easily sum up values in different columns?
  • Reading numeric Date value from CSV file to data.frame in "R"
  • R programming: creating a stacked bar graph, with variable colors for each stacked bar
  • How to identify all columns that contain binary representation
  • Filter different groups by different factor levels
  • Saving .xlsx file to disc, form http post request
  • Add an "all" option under the filter that selects the number of rows displayed in a datatable
  • How to select second column of every xts in list
  • Generate a frequency dataframe out of an input dataframe
  • Why manual autocorrelation does not match acf() results?
  • Merge 3 dataframes which are different to each other
  • remove adjacent duplicates from string
  • How to change the position of stacked stacked bar chart in ggplot in R?
  • How to divide each of a range a variables by a second range of variables in R
  • Why do I need to assemble vector before scaling in Spark?
  • How to select individuals which appear in multiple groups?
  • How can I fill columns based on values in another column?
  • 32 bit R and 64 bit R: output differs
  • Remove a single backslash in paste0 output
  • ggplot2 different label for the first break
  • TSP in R, with given distances
  • How to find the given value from the range of values?
  • Solution on R group by issue _ multiple combination
  • Transform multiple columns with a function that uses different arguments per column
  • How can I parse a string with the format "1/16/2019 1:24:51" into a POSIXct or other date variable?
  • How to plot a box plot in R for outlier detection for a huge number of rows?
  • How to change column name according to another dataframe in R?
  • `sjPlot::tab_df()`--how to set the number of decimal places?
  • time average for specific time range in r
  • joining dataframes by closest time and another key in r
  • How to create nested for loop for a certain range
  • New category based on sequence of date ranges
  • how to extract formula from coxph model summary in R?
  • add row based on variable condition in R
  • Generating the sequence 111122222333334
  • Unable to use has_goog_key() in R
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com