ggplot2

Plot time series with years in different columns

流过昼夜 提交于 2021-02-17 07:06:20
问题 I have the following data frame dt(head,6) : I need to create a graph in which I have the years (2015, 2016, 2017, 2018, 2019) on the x-axis , different columns (W15, W16, W17, W18, W19 - each one relates to one year) on the y-axis. They are all should be grouped by the column TEAM. I tried using ggplot2 to no avail. 回答1: You need to convert your data from wide to long and then use ggplot . Look below; library(tidyverse) dt %>% pivot_longer(., -Team, values_to = "W", names_to = "Year") %>%

Different x and y axis scales in multifaceted scatter ggplot2

谁都会走 提交于 2021-02-17 06:46:20
问题 I have used lemon package with ggplot2 for plotting multifaceted scatter plot with regression and confidence interval line using the following code library(tidyverse) library(lemon) #Plotting ggplot(data_calibration, aes(Observed,Predicted))+ geom_point(color="black",alpha = 1/3) + facet_rep_grid(Station ~ Method, scales="free", repeat.tick.labels = "all")+ xlab("Measured") + ylab("Predicted")+ theme_bw()+ geom_smooth(method="lm") I want to have both x and y-axis scales to be free. But I am

Inverse of ggplotGrob?

三世轮回 提交于 2021-02-17 06:30:10
问题 I have a function which manipulates a ggplot object, by converting it to a grob and then modifying the layers. I would like the function to return a ggplot object not a grob. Is there a simple way to convert a grob back to gg? The documentation on ggplotGrob is awfully sparse. Simple example: P <- ggplot(iris) + geom_bar(aes(x=Species, y=Petal.Width), stat="identity") G <- ggplotGrob(P) ... some manipulation to G ... ## DESIRED: P2 <- inverse_of_ggplotGrob(G) such that, we can continue to use

how to make stacked barplot with defined horizontal borders for each bar

风格不统一 提交于 2021-02-17 06:26:26
问题 I have data that i want to show in barplot in a way that i could'nt figure out how. Hope you can help me with this! My table consists of 4 columns: cluster (0:6), IgG_Status (mild_high, mild_low,Severe_High), patient (1-16) and value (normalized value per each cluster). These are the lines of code i'm using now to create a barplot of the sum of values for each cluster, divided to IgG_Status (dodged style). ggplot(mat, aes(x= cluster, fill= IgG_status, group=IgG_status)) + geom_bar(aes(weight

difference between the two ways of using aes in ggplot?

谁说胖子不能爱 提交于 2021-02-17 06:21:25
问题 I recently started learning R but am confused with the aes feature in ggplot2. I have seen two different places where aes is placed in the code. ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) ggplot(mpg, aes(x = displ, y = hwy)) + geom_point() What is the difference between the two? 回答1: Can't find a dupe, so here's an answer: Aesthetics specified in ggplot() are inherited by subsequent layers. Aesthetics specified in particular layers are specific only to that layer. Here

ggplot2 add manual legend for two data series

耗尽温柔 提交于 2021-02-17 05:39:10
问题 I have this dataframe: Control Stress days sd_control sd_stress X1 0.9702100 0.9343627 X1 0.001900535 0.07035645 X2 0.9666619 0.8595523 X2 0.014946893 0.04066567 X3 0.9165654 0.7160598 X3 0.072655343 0.07025344 X4 0.9208237 0.6668044 X4 0.050870831 0.08736982 X5 0.8766547 0.7660685 X5 0.073588197 0.04868614 X6 0.9599553 0.7937444 X6 0.041559836 0.05326769 X7 0.9736297 0.8188934 X7 0.003817743 0.06272428 and based on this data I've done this plot: With the following code: significance <- data

ggplot2 add manual legend for two data series

痞子三分冷 提交于 2021-02-17 05:39:05
问题 I have this dataframe: Control Stress days sd_control sd_stress X1 0.9702100 0.9343627 X1 0.001900535 0.07035645 X2 0.9666619 0.8595523 X2 0.014946893 0.04066567 X3 0.9165654 0.7160598 X3 0.072655343 0.07025344 X4 0.9208237 0.6668044 X4 0.050870831 0.08736982 X5 0.8766547 0.7660685 X5 0.073588197 0.04868614 X6 0.9599553 0.7937444 X6 0.041559836 0.05326769 X7 0.9736297 0.8188934 X7 0.003817743 0.06272428 and based on this data I've done this plot: With the following code: significance <- data

specific country map with district/cities using R

蓝咒 提交于 2021-02-17 05:33:23
问题 I am trying to draw some specific countries map such as, Bangladesh, Bhutan etc. with its district/cities in R. As an example, I can draw US map using the following lines of codes. Is there any such library/package that can give me any countries map with its cities/district/province? Any clue is appreciated. library(maps) states <- map_data("state") 回答1: You can download shapefile of any country from the following website https://www.diva-gis.org/gdata Then read and plot them in R using

How can I solve non-finite location and/or size for viewport error?

白昼怎懂夜的黑 提交于 2021-02-17 04:52:13
问题 Here is a simplified and testable example: dataset <- data.frame( emp_month = c("January","March","April","May","December"), salary = c(623.3,515.2,611.0,729.0,843.25)) library(ggplot2) ggplot(dataset)+ geom_boxplot(aes(x = sort(factor(emp_month)), y = salary))+ geom_point(aes( x = sort(factor(emp_month)), y=salary))+ facet_grid(. ~ sort(factor(emp_month)),space = "free", scales="free",margins = T) Error Description: I can write this code library(ggplot2) MesDeConclusao = factor

Format numbers on axes and tooltips in ggplotly

▼魔方 西西 提交于 2021-02-17 04:52:07
问题 I have used sprintf and formatC to take a double value and round it to two decimal places. However, when I go to use it in ggplot and ggplotly, it makes my visuals act out. Dput: structure(list(Date = structure(c(18328, 18329, 18330, 18331, 18332, 18333), class = "Date"), State = c("Louisiana", "Louisiana", "Louisiana", "Louisiana", "Louisiana", "Louisiana"), variablename1 = c(0, 0, 1, 1, 6, 14), variablename2 = c(5, 5, 5, 11, 37, 37), death = c(0, 0, 0, 0, 0, 0), variablename3 = c(5, 5, 6,