ggplot2

How do I transform a data frame and make multiple line graphs in R?

余生长醉 提交于 2021-02-08 10:30:12
问题 I have a data frame as below 0-10 10-20 20-30 30-40 M 1 2.5 4 0.3 N 0.5 0.8 2.3 1 The 0-10, 10-20 intervals is my first row without any column headings. I want to plot two line graphs on the same graph depicting M and N values on y-axis and the intervals 0-10, 10-20 etc. as my X-axis. How do I go about doing that? If I make the intervals as my heading, then I am unable to plot these graphs using ggplot as I do not have a x variable for aes() 回答1: A bit hard without the data, but I've created

second y-axis with other scale [duplicate]

ぐ巨炮叔叔 提交于 2021-02-08 10:28:50
问题 This question already has answers here : ggplot with 2 y axes on each side and different scales (17 answers) Closed 4 days ago . I created a barchart with ggplot2 geom_bar and want to have two metrics in the bars. I used melt to do so. However, I now need a second y-axis with another scale, because the numbers of the two metrics are too different. In the following the dataframe and the code I used: df <- data.frame(categories = c("politics", "local", "economy", "cultural events", "politics",

How to supply variable names as strings in fct_reorder within ggplot code?

江枫思渺然 提交于 2021-02-08 10:24:09
问题 I want to create the geom_col chart by supplying the variable names as strings. I know the aes_string does that in the ggplot code. However, I am not sure how to handle the variables supplied in the fct_reorder to order the bars. Here is my wip code. I want to convert this: mpg %>% ggplot() + geom_col(aes(x = cty, y = fct_reorder(drv, cty, .fun = mean, na.rm = T), fill = drv), width = 0.8) + theme_classic() into a function, chart_fun <- function(x, y) { mpg %>% ggplot() + geom_col(aes_string

subway-style graph for word frequency across three datasets in ggplot2

家住魔仙堡 提交于 2021-02-08 10:01:43
问题 this question is a followup from https://stackoverflow.com/a/64991805?noredirect=1 i have a dataset with one dictionary, dict, and the word frequencies of each word within dictionary from dataset, dictfreq$freq_gov, dictfreq$freq_indiv, dictfreq$freq_media. dict: apple, pear, pineapple freq_gov: 12, 13, 10 freq_indiv: 11, 20, 1 freq_media: 13, 21, 9 desired output looks like this: https://blog.revolutionanalytics.com/2015/12/r-is-the-fastest-growing-language-on-stackoverflow.html where y-axis

Make a new scale in ggplot2

China☆狼群 提交于 2021-02-08 09:39:18
问题 I have the following plot which plots quantile labels and breaks. This is currently done manually. library(tidyverse) mtcars %>% as_tibble() %>% ggplot(aes(y = mpg, x = hp, color = factor(cyl))) + geom_point() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank()) + scale_y_continuous(labels = as.numeric(quantile(mtcars$mpg)), breaks = as.numeric(quantile(mtcars$mpg))) + scale_x_continuous(labels = as

ggplot2: How do you select a subset of factor levels to be grouped into a single facet

丶灬走出姿态 提交于 2021-02-08 09:13:43
问题 I want to compare one level of a variable against the combined influence of all other variables. I would like to do this with a facet plot. For instance: ggplot(diamonds, aes(price, colour = cut)) + geom_density() + facet_grid(~clarity) This provides a faceted plot of all the factor levels in clarity. However, what I would like to have is a density plot of I1 in the first facet and a density plot of ~(I1) in the second facet. So I would like to produce a comparison of the following using the

ggplot2: How do you select a subset of factor levels to be grouped into a single facet

房东的猫 提交于 2021-02-08 09:11:52
问题 I want to compare one level of a variable against the combined influence of all other variables. I would like to do this with a facet plot. For instance: ggplot(diamonds, aes(price, colour = cut)) + geom_density() + facet_grid(~clarity) This provides a faceted plot of all the factor levels in clarity. However, what I would like to have is a density plot of I1 in the first facet and a density plot of ~(I1) in the second facet. So I would like to produce a comparison of the following using the

Gap in polar time plot - How to connect start and end points in geom_line or remove blank space

帅比萌擦擦* 提交于 2021-02-08 09:00:55
问题 I am still new to ggplot2. I want to plot my data from my insect poo experiment: x-value is time of day (24h) and y value is amount of poo (frass) produced as a percentage and represented as a circular plot. I added geom_ribbon to represent standard deviation which I calculated with circular. Firstly, I had issues with the 'clock' starting as 1/24 instead of 0/24: ggplot2 clock starting from 1/24 instead of 0/24: So I added the code expand_limits(x = 0, y = 0) which helped with fixing 1/24 to

stacked barplot with aggregated data (ggplot2)

隐身守侯 提交于 2021-02-08 08:21:30
问题 I have some major problems with ggplot2. Even though it might be a very easy question to you I couldnt manage yet to get it right (I have read a ggplot2-book and was looking on stackoverflow as well). Orginally there was a dataset consisting of a factor variable (country) and a dichotomous variable. Unfortunately I dont have my data in this extended format myself: I have two variables "var1" and "var2". var1 gives the number of cases in the original dataset where a certain condition is true

seasonal ggplot in R?

强颜欢笑 提交于 2021-02-08 08:17:00
问题 I am looking at data from Nov to April and would like to have a plot starting from Nov to April. Below is my sample code to screen out month of interests. library(tidyverse) mydata = data.frame(seq(as.Date("2010-01-01"), to=as.Date("2011-12-31"),by="days"), A = runif(730,10,50)) colnames(mydata) = c("Date", "A") DF = mydata %>% mutate(Year = year(Date), Month = month(Date), Day = day(Date)) %>% filter(Month == 11 | Month == 12 | Month == 01 | Month == 02 | Month == 03 | Month == 04) I tried