Changing linetype with modelplot - plot

I am trying to change the linetypes of my regression plot with model plot.
I have several indices, displayed for several groups(all participants, only women, only men and different age groups).
There is no error message, but somehow, the linetype argument is not applied. However, the color argument works. Did that happen to anyone before or can you find my mistake?
This is my code, for the gender differentiation:
#Plots for all index, #2 for gender (sex.V0)
models7 <- list("Social Index Female"=ols_women, "Social Index Male"=ols_men, "Trust Index Female"=ols_women_trust, "Trust Index Male"=ols_men_trust, "Identification Index Female"=ols_women_id, "Identification Index Male"=ols_men_id, "Motivation Index Female"=ols_women_mot, "Motivation Index Male"=ols_men_mot, "Engagement Index Female"=ols_women_eng, "Engagement Index Male"=ols_men_eng)
b <-list(geom_vline(xintercept = 0, color='yellow'), scale_color_manual(values=c("Social Index Female"='red', "Social Index Male"='red', "Trust Index Female"='orange', "Trust Index Male"='orange', "Identification Index Female"='green', "Identification Index Male"='green', "Motivation Index Female"='blue', "Motivation Index Male"='blue', "Engagement Index Female"='purple', "Engagement Index Male"='purple'), aesthetics="color"), scale_linetype_manual(values=c("Social Index Female"='solid', "Social Index Male"='dashed', "Trust Index Female"='solid', "Trust Index Male"='dashed', "Identification Index Female"='solid', "Identification Index Male"='dashed', "Motivation Index Female"='solid', "Motivation Index Male"='dashed', "Engagement Index Female"='solid', "Engagement Index Male"='dashed')))
modelplot(models7, background= b, coef_omit = "Intercept", coef_rename = ("tx.V0"="TE"))+ labs(title="Coefficient Plots Gender")+theme_classic()
ggsave("plot_gender.png")
Thanks!!
[enter image description here](https://i.stack.imgur.com/qMPqy.png)
this is the resulting plot!

Related

How to count number of entries on a dataset based on given conditions and assign those results into a matrix using for loops?

I have a dataset with 50 unique entries, and each entries has some hundreds of in favor and against votes for each of those elements (which I set as 1 and 0). I want to calculate the number of 1s and 0s per element.
I know that if I subset each entries I can find the counts I am looking for, but since there're several other conditions I want to count too, making a for loop would save me time.
So I did:
matrix_name <- matrix(NA, ncol = 6, nrow = 50)
colnames(matrix_name) <- c(xyz)
# I have the first column for the entries, the second for "Yes", the third for "No", and the remaining some extra conditions
matrix_entries <- unique(df$entries)
names(matrix_name[,]) <- matrix_entries
for(i in 1:50){
matrix_name[i] <- matrix_entries[i]
}
With this I am able to assign the entry name in the first column, but I can't proceed with the counting (that would go in the remaining columns) using a loop (which I would substitute the bunch of NAs that I have).
What I did to count the number of Yes and No for each entry was:
count(subset(df, entry == "entry #1" & vote == "1"))
count(subset(df, entry == "entry #1" & vote == "0"))
But I'm not sure if this is the most efficient way to count those entries, and how to use a for loop in this situation.
I appreciate your time for replying.
Edit: Here goes the subset with relevant information:
structure(list(`Entry;Name;Vote` = c("Entry #1;Alex;0", "Entry #1;John;0",
"Entry #1;Tom;1", "Entry #1;Phillip;0", "Entry #1;Mary;0", "Entry #1;Anna;0",
"Entry #1;Peter;0", "Entry #1;Erika;1", "Entry #1;Louise;0",
"Entry #1;Arthur;0")), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))

Change column values depending on other column in R

I have problem with my data frame.
I have a dataframe with 2 columns, 'word' and 'word_categories'. I created different variables which include the different words, e.g. 'noun' which includes all the nouns of the word column. I now want to change the labels in the word_categories column to the corresponding variable. So if the word in the word column is included in the object 'noun', I want the word_categories column to display 'noun'.
df <- read.csv("palm.csv")
noun <- c("house", ...)
adj <- c("hard", ...)
...
The data frame looks like the following. It includes other columns but they are fine.
word word_categories
house
car
hard
...
I now want to look, if the words are in any of the created objects and if so, I want the corresponding label printed in the word_categories column. So for 'house' the column should show noun, for 'hard' it should show adjective. If the word is in none of the objects, it should show nothing or 'NA'.
I tried it with the following:
palm$word_categories <- ifelse(palm$word == noun, "noun",
ifelse(palm$word == adj, "adjective", "")))
This, however, doesn't work at all and I have 7 Objects in total so the statement becomes ridiculously long. How do I do it properly?
If the dataframe is called palm (you first call it df but later you use palm) and noun and adj are vectors as you define above, I would do:
library(dplyr)
palm <- palm %>%
mutate(word_categories = case_when(word %in% noun ~ "noun",
word %in% adj ~ "adjective",
TRUE ~ NA_character_))
One way would be to create a named vector of your noun/adjective dictionaries to select each element. The name would be the word and the corresponding data would be noun, adjective etc. You didn't really supply any data so I made some up.
df <- data.frame(
stringsAsFactors = FALSE,
word = c("dog", "short", "bird", "cat", "short", "man")
)
nounName <- c('dog', 'cat', 'bird')
adjName <- c('quick', 'brown', 'short')
noun <- rep('noun', length(nounName))
adj <- rep('adjective', length(adjName))
names(noun) <- nounName
names(adj) <- adjName
partsofspeech <- c(noun, adj)
df$word_categories <- partsofspeech[df$word]

merge dataframe containing different case in R

I am having an issue where I need to merge two dataframes but the common column has different cases (some have upper case and some have lower case)
Example Data:
authors <- data.frame(
surname = I(c("Tukey", "Venables", "Tierney", "Ripley", "McNeil")),
nationality = c("US", "Australia", "US", "UK", "Australia"),
deceased = c("yes", rep("no", 4)))
books <- data.frame(
name = I(c("tukey", "venables", "tierney",
"tipley", "ripley", "McNeil", "R Core")),
title = c("Exploratory Data Analysis",
"Modern Applied Statistics ...",
"LISP-STAT",
"Spatial Statistics", "Stochastic Simulation",
"Interactive Data Analysis",
"An Introduction to R"),
other.author = c(NA, "Ripley", NA, NA, NA, NA,
"Venables & Smith"))
m1 <- merge(authors, books, by.x = "surname", by.y = "name")
Data is taken from this question
I need to produce a result without changing the data i.e.
1) I do not have access to create a new column in the dataframe or
2) change the case in the dataframe or
3) create a new dataframe.
I understand that R is case dependent but some help would be really appreciated. Thanks.
Assuming that you can create temporary dataframes, I would do the following.
Store your data in temporary dataframes.
Create temporary columns with the transformations mentioned in your example.
Merge the temporary dataframes based on the transformations.
Remove unnecessary variables.
Translated in R, that gives.
library(stringr)
books_temp <- books # store in temporary df
authors_temp <- authors
authors_temp$surname_temp <- str_to_title(authors_temp$surname) # transform columns
books_temp$name_temp <- str_to_title(books_temp$name)
m1 <- merge(authors_temp, books_temp, by.x = "surname_temp", by.y = "name_temp") # merge
m1$surname_temp <- NULL # discard unnecessary information
rm(authors_temp)
rm(books_temp)
Note that it would be very hard to merge two dataframes containing information that require treatments without storing the intermediate transformations somewhere.

Assign values conditionally in R

I need to create a new variable and assign values to the row based on another categorical variable. The data table looks like this
Specifically, I want to create a variable called channel_num. If the strings in channelGrouping equal to "Direct", "Display" and "Paid Search", I will assign 0 to this row; if they equal to "Organic Search" and "Social", I will assign 1.
Assuming your datatable is named df:
df$channel_num <- ifelse(df$channelGrouping %in% c("Direct","Display","Paid Search"), 0, ifelse(df$channelGrouping %in% c("Organic Search","Social"), 1, NA))

R: Graphing a table plan

General Goal
I am placing guests around tables according to a set of rules. My goal, in this post, is to have a handy function to display the names of these guests around their respective tables on a R graphic.
Input Data
guests describes the position of each guest at each table. Note that the number of guests per table varies.
guests = list(
table_1 = c("Jack", "Christelle", "Frank", "John S.", "Lucia"),
table_2 = c("George", "Amanda", "Alice", "Laura", "John H."),
table_3 = c("Jeanette", "Elizabeth", "Remi", "Fabian", "Urs", "Emma"),
table_5 = c("Roger", "Marry", "Henrique", "Claire", "Julia"),
table_6 = c("Alphonse", "Marie", "Dani", "Rachel")
)
Table_positions indicate where each table should be positioned on the graph. I assume here that each axis goes from 0 to 10, where the point c(5,5) is at the center of the graph.
Table_positions = data.frame(
y_position=c(3,2,3,7,8,7),
x_position=c(3,5,7,3,5,7)
)
Details of the graphic
I suggest that the tables are represented by circles centered at the position indicated by the data.frame Table_positions. The names of each guest should be written around these tables following the list guests.
Placement of Tables :
require(plotrix)
plot(x = Table_positions$x_position
,y= Table_positions$y_position
,xlim=c(0,10),ylim=c(0,10),pch=".")
draw.circle(Table_positions$x_position,
radius=0.5,
Table_positions$y_position)
Guests Positioning :
for(i in 1:length(guests)){
Table<-as.vector(unlist(guests[i]))
posTable<-c(Table_positions$x_position[i],Table_positions$y_position[i])
nbGuest<-length(Table)
for(j in 1:nbGuest){
text(x=posTable[1]+round(0.5*cos((2*j/nbGuest)*pi),2),
y=posTable[2]+round(0.5*sin((2*j/nbGuest)*pi),2),
labels=guests[[i]][[j]],
cex=0.5)
}
}
I added table4 with one pepole named "Bla".
You can specify the text size with cex option (here 0.5).

Resources