ggplot2

R ggplot2 - Simple plot- cannot specify log axis limits

时间秒杀一切 提交于 2021-02-07 14:20:35
问题 I'm trying to create a simple densityplot in R in ggplot2. Here's my code which works great. d <- ggplot(result, aes(x=result$baseMeanA)) d + geom_density(colour="darkgreen", size=2, fill="darkgreen") + scale_x_log10() + scale_y_continuous(limits = c(0, 0.45)) The problem is that I cannot adjust the x-axis as I would like, into negative numbers. scale_x_log10(limits= c(1, 10000)) works great, but scale_x_log10(limits= c(-1, 10000)) does not work at all! It gives me this error: Error in if

ggplot2 density of circular data

橙三吉。 提交于 2021-02-07 14:13:14
问题 I have a data set where x represents day of year (say birthdays) and I want to create a density graph of this. Further, since I have some grouping information (say boys or girls), I want to use the capabilities of ggplot2 to make a density plot. Easy enough at first: require(ggplot2); require(dplyr) bdays <- data.frame(gender = sample(c('M', 'F'), 100, replace = T), bday = sample(1:365, 100, replace = T)) bdays %>% ggplot(aes(x = bday)) + geom_density(aes(color = factor(gender))) However,

ggplot2 density of circular data

时光怂恿深爱的人放手 提交于 2021-02-07 14:11:44
问题 I have a data set where x represents day of year (say birthdays) and I want to create a density graph of this. Further, since I have some grouping information (say boys or girls), I want to use the capabilities of ggplot2 to make a density plot. Easy enough at first: require(ggplot2); require(dplyr) bdays <- data.frame(gender = sample(c('M', 'F'), 100, replace = T), bday = sample(1:365, 100, replace = T)) bdays %>% ggplot(aes(x = bday)) + geom_density(aes(color = factor(gender))) However,

How to detect if bare variable or string

南笙酒味 提交于 2021-02-07 13:51:06
问题 I am trying to write a plotting function where you can pass bare column names to select which columns are plotted. I would like also to be able to specify a string as the color. I have found that I need to use shQuote if I want to pass a string to aes_string. Now my problem is to figure out if a bare name or a string was passed. How would I do this? dat <- data.frame( time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")), total_bill = c(14.89, 17.23) ) plot_it <- function(dat, x,y,

R pie charts distorted when adding to projected map using ggplot

孤者浪人 提交于 2021-02-07 13:22:53
问题 I want to plot pie charts onto a projected map using ggplot. However, the pie charts become distorted, probably due to the projection. Does anyone know how I can plot the pie charts without the distortion? Example code is below, thanks. lib = c("ggplot2","scatterpie") lapply(lib, library, character.only=TRUE) pie = data.frame( lon=c(-5.0,-3.5,-5.5,5.0), lat=c(50.0,50.2,50.1,50.5), A=c(0.25,0.75,0,0.25), B=c(0.75,0.10,0,0.75), C=c(0,0.15,1,0), radius=0.05) world = map_data("world", resolution

How to mimic geom_boxplot() with outliers using geom_boxplot(stat = “identity”)

霸气de小男生 提交于 2021-02-07 12:38:21
问题 I would like to pre-compute by-variable summaries of data (with plyr and passing a quantile function) and then plot with geom_boxplot(stat = "identity") . This works great except it (a) does not plot outliers as points and (b) extends the "whiskers" to the max and min of the data being plotted. Example: library(plyr) library(ggplot2) set.seed(4) df <- data.frame(fact = sample(letters[1:2], 12, replace = TRUE), val = c(1:10, 100, 101)) df # fact val # 1 b 1 # 2 a 2 # 3 a 3 # 4 a 4 # 5 b 5 # 6

altering the color of one value in a ggplot histogram

試著忘記壹切 提交于 2021-02-07 10:50:36
问题 I have a simplified dataframe library(ggplot2) df <- data.frame(wins=c(1,1,3,1,1,2,1,2,1,1,1,3)) ggplot(df,aes(x=wins))+geom_histogram(binwidth=0.5,fill="red") I would like to get the final value in the sequence,3, shown with either a different fill or alpha. One way to identify its value is tail(df,1)$wins In addition, I would like to have the histogram bars shifted so that they are centered over the number. I tried unsuccesfully subtracting from the wins value 回答1: 1) To draw bins in

ggplotly and multiple highlight functions

时光总嘲笑我的痴心妄想 提交于 2021-02-07 10:31:15
问题 How can I use two or more highlight functions on a ggplotly object. In the following example I want to highlight the hovered bar in black the clicked bar in blue When using 2 highlight functions, only the last one is used and it obviously overwrites the previous one. So how can I define different behaviour for click and hover-events? Data: dnew <- {structure(list(time_stamp = structure(c(1514761200, 1514847600, 1514934000, 1515020400, 1515106800, 1515193200, 1515279600, 1515366000, 1515452400

ggplotly and multiple highlight functions

柔情痞子 提交于 2021-02-07 10:31:12
问题 How can I use two or more highlight functions on a ggplotly object. In the following example I want to highlight the hovered bar in black the clicked bar in blue When using 2 highlight functions, only the last one is used and it obviously overwrites the previous one. So how can I define different behaviour for click and hover-events? Data: dnew <- {structure(list(time_stamp = structure(c(1514761200, 1514847600, 1514934000, 1515020400, 1515106800, 1515193200, 1515279600, 1515366000, 1515452400

Reorder columns in boxplots ggplot2

北城以北 提交于 2021-02-07 10:30:59
问题 I have this boxplot and I want to reorder the columns. Instead of MAR, MCAR, MNAR, I want MCAR, MAR, MNAR. And the legend is wrong too, orange is ymiss and blue is yobs but I can't change it. Can you help me? thanks! library(ggplot2) logistic <- function(x) exp(x) / (1 + exp(x)) set.seed(80122) n <- 300 dt <- MASS::mvrnorm(n = n, mu = c(0, 0), Sigma = matrix(c(1, 0.5, 0.5, 1), nrow = 2));dt r2.mcar <- 1 - rbinom(n, 1, 0.5);r2.mcar r2.mar <- 1 - rbinom(n, 1, logistic(dt[, 1]));r2.mar r2.mnar <