ggplot scale_size_manual function doesn't work - r

I'm new to ggplot, and I'm not sure how to properly use scale_size_manual on groups of data. I tried to show the label at the right of graph, for example "0-200000", "800000+", but it does not show up on the graph.
data %>%
ggplot(aes(year1, imdb_rating)) +
geom_point(aes(colour = multi_language, size = imdb_votes1)) +
scale_size(breaks = c(1,2,3,4),
labels = c('0-200000','200000-400000','400000-800000','800000+'))
enter image description here
if i use scale_size, this is how it shows. If i change scale_size to scale_size_manual, it won't output anything and shows error "
Error: Continuous value supplied to discrete scale"
the glimpse value of imdb_votes1 is
glimpse(data$imdb_votes1) num [1:269] 7993 37573 24169 6725 22375 ...
The data$imdb_votes1 is a continuous variable,so I'm not sure how to deal with it. Any help is appreciated, thank you so much
enter image description here
this is how i want it to be shown, except i change the label and name

Related

Formatting changes affect only legend and not bar graph using swimplot and ggplot2 packages

Update- this issue was solved, updated code is at the end of the post.
I am trying to create a swimmer plot to visualize individual patient duration of treatment with a drug administered at multiple dose levels (DLs). Each patient will be be assigned to treatment with only one DL, but multiple patients can be assigned to a given DL (e.g. 3 patients at DL1, 3 patients and DL2, etc.). I would like to color code the bars in the swimmer plot according to DL.
I am using the swimplot package for R and have been following the guide located here (https://cran.r-project.org/web/packages/swimplot/vignettes/Introduction.to.swimplot.html).
This guide has been sufficient for most things I have tried, up until I tried to change the colors of the bars in the plot and corresponding legend. Following the section in that guide titled "Modifying Colours and shapes" under "Making the plots more aesthetically pleasing with ggplot manipulations", I was able to change the bar colors in the legend, but not the bars themselves.
Example here
I have been using the following code.
library(ggplot2)
library (swimplot)
library (gdata)
library (readxl)
ClinicalTrial.Arm <- read_excel("Swimmer_Test_Data1.xls")
ClinicalTrial.Arm <- as.data.frame(ClinicalTrial.Arm)
arm_plot <- swimmer_plot(df=ClinicalTrial.Arm,id='id',end='End_trt',width=.85+ scale_fill_manual(name="Arm",values=c("DL1" ="#003f5c", "DL2"="#374c80","DL3"="#7a5195","DL4"="#bc5090","DL5"="#ef5675","DL6"="#ff764a","DL7"="#ffa600"))+ scale_color_manual(name="Arm",values=c("DL1" ="#003f5c", "DL2"="#374c80","DL3"="#7a5195","DL4"="#bc5090","DL5"="#ef5675","DL6"="#ff764a","DL7"="#ffa600"))
arm_plot
I have tried a number of things to fix this, but am quite new to R and don't think I really know enough to troubleshoot effectively. I have tried various syntax changes (e.g. removing quotation marks) and have tried using the geom bar command but wasn't sure how/what to map to X and Y (it also seems like I shouldn't need to do this).
I have also tried using the following code, but get an error.
Colors <- c("DL1" ="#003f5c", "DL2"="#374c80","DL3"="#7a5195","DL4"="#bc5090","DL5"="#ef5675","DL6"="#ff764a","DL7"="#ffa600")
arm_plot <- swimmer_plot(df=ClinicalTrial.Arm,id='id',end='End_trt',width=.85, fill = Colors)+ scale_fill_manual(name="Arm",values=c("DL1" ="#003f5c", "DL2"="#374c80","DL3"="#7a5195","DL4"="#bc5090","DL5"="#ef5675","DL6"="#ff764a","DL7"="#ffa600"))+ scale_color_manual(name="Arm",values=c("DL1" ="#003f5c", "DL2"="#374c80","DL3"="#7a5195","DL4"="#bc5090","DL5"="#ef5675","DL6"="#ff764a","DL7"="#ffa600"))
Error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (20): fill
Run `rlang::last_error()` to see where the error occurred.
Any help here would be greatly appreciated.
Solved! Updated, working code
library(ggplot2)
library (swimplot)
library (gdata)
library (readxl)
ClinicalTrial.Arm <- read_excel("Swimmer_Test_Data1.xls")
ClinicalTrial.Arm <- as.data.frame(ClinicalTrial.Arm)
Colors <- c("DL1" ="#003f5c", "DL2"="#374c80","DL3"="#7a5195","DL4"="#bc5090","DL5"="#ef5675","DL6"="#ff764a","DL7"="#ffa600")
arm_plot <- swimmer_plot(df=ClinicalTrial.Arm,id='id',end='End_trt', name_fill = "Arm", width=.85) + scale_fill_manual(name="Arm",values = Colors) +
scale_color_manual(name="Arm",values=Colors)
To make your code work you first have to map a variable on the fill aesthetic which using swimplot could be achieved via the name_fill argument:
Note: As I use the ClinicalTrial.Arm dataset from the swimplot package I adjusted your color palette to make it work with the three categories of the Arm column in this dataset.
library(ggplot2)
library(swimplot)
#pal <- c("DL1" = "#003f5c", "DL2" = "#374c80", "DL3" = "#7a5195", "DL4" = "#bc5090", "DL5" = "#ef5675", "DL6" = "#ff764a", "DL7" = "#ffa600")
pal <- c("Arm A" = "#003f5c", "Arm B" = "#bc5090", "Off Treatment" = "#ffa600")
swimmer_plot(df = ClinicalTrial.Arm, id = "id", end = "End_trt", name_fill = "Arm", width = .85) +
scale_fill_manual(name = "Arm", values = pal)

Change size of label annotations in a ggplot

I am trying to change text label sizes inside my plot (not the axes, rather the label annotations)
I am working with a phyloseq object but I don't think that matters.
Here is the code and the output. Any suggestions?
plot_ordination(prokaryote_ra, ordBC, color = "Stage", label="SampleID") + ggtitle("PCoA: Bray-Curtis")
graph of ordination plot with label annotations that are too small
Looks like size for the text label is a fixed value 2, in the plot_ordination() function see L1135 at GitHub in plot-methods.R file
...
p = p + geom_text(label_map, data=rm.na.phyloseq(DF, label),
size=2, vjust=1.5, na.rm=TRUE) # <--- here size is set to 2
...
Solution would be either submit an issue at GitHub, and wait for an update from maintainer, or copy paste the function locally, and change the size to match your needs.

R grouped/centered barplot with different fill with ggplot2

I have the following dataset:
db1.1 <- data.frame(Status1.1 = rep(c("Completed", "Ongoing"), each=9),code1.1= rep(c(1:9), times=2), nProj1.1 = c(-24,-2,-17,-59,-1,-12,-6,0,0,0,2,3,5,0,2,0,1,1))
With this dataset, I build a graphic very similar to this one (code1.1 is the x axis, nProj1.1 is the y axis, and Status1.1 gives the two different grey tones):
I used this code to build the graphic:
ggplot(db1.1, aes(x=code1.1, y=nProj1.1, fill=Status1.1)) + geom_bar(stat="identity", position="identity")+coord_flip()+geom_hline(yintercept = 0, size=1)
However, I want to add a new variable/overlap a graphic, to obtain the following result:
Basically, it is the same as the one above but with values over the grey bars, with the dashed lines.
I have a new dataset that should correspond to the bars with dashed lines, with the same variables:
db1.2 <- data.frame(Status1.2 = rep(c("Completed", "Ongoing"), each=9),code1.2= rep(c(1:9), times=2), nProj1.2 = c(0,0,-14,-43,-1,-10,-5,0,0,0,2,3,5,0,1,0,0,1)) # manter assim, que já atribui a classe a cada variavel; ex.: factor, num, int, etc
I tried following this question: R-stacked-grouped barplot with different fill in R , but I didn't manage yet to make it work. I can also group both datasets and create a new binary variable, but I am not sure if that would help.
Does anyone know how can I make this kind of graph?

R, tmap: Changing labels in view mode

I'm working on a map with tmap and i'm having some trouble changing labels in view mode as in
here.
I'm trying to change it to cities names column using name argument like:
tm_shape(mymap, name = "city_name") +
tm_polygons("Inad", palette = "Reds", title = "")
but it keeps showing the first column as label, which is cities codes (320120 for example).
Is name the right argument to accomplish so?
You can use the id argument:
tm_shape(mymap) + tm_polygons("Inad", id="city_name" palette="Reds", title="")
The following is likely to do the trick:
tm_shape(mymap) +
tm_text(text = name_var_in_your_shapefile)
If you supply a sample link or other way for replicating, we can test it out. For documentation on tm_text have a look here

geom_text label with by the use of a numerical variable

I'd like to label text (it's a variable) using geom_text:
geom_text(aes(as.Date("2018-03-05"), -8), label = A_Statrd)
I get this error:
Fehler: Aesthetics must be either length 1 or the same as the data (31): label
How can I treat a variable as a label?
Text works well. When I use quotation marks, he will write me the text into my graphic. But this is not what I want. I'd like to take a variable. The variable is numeric.
For any tips/advice I thank you very much.
geom_text is going to expect label as an aesthetic, so it expects it will be receiving a column of your dataframe---hence the error about the vector length.
Two options:
One: make a dummy dataframe with this label in it, something like
data.frame(x = as.Date("2018-03-05"), y = -8, label = A_Statrd)
and use that as the data parameter in your call to geom_text.
Or two: the better option, as suggested in one of the comments, is to use annotate instead of geom_text. This is the situation annotate was designed for, where you have a bit of text or other annotation that isn't mapped to your data, but is instead some static piece of information. Use something like
ggplot(...) +
annotate(geom = "text", x = as.Date("2018-03-05"), y = -8, label = A_Statrd)
This assumes that A_Statrd is a variable you have stored elsewhere---that's what it seems like from your question.

Resources