How do I add colour based on values in gt table? - gt

I've generated the following gt table and I was wondering how to add blue colour to the cells under the Neurons, Astro, Oligo, Micro and Endothelia columns. I would want the shade of blue to get darker the larger the value. I have tried everything but it just isn't working. Also, where can I see what colours/shades are available? Thank you advance!!
The table generated
the code I used:
# read in the subject metadata file that has sample/experimental labels
decon_CIB_IP <- read.csv("CIBERSORT_IP_dataset.csv", row.names=NULL, fill=TRUE)
gt_tbl <- gt(decon_CIB_IP)
# Generate a simple table with a stub
# and add a stubhead label
gt_tbl <-
gt_tbl %>%
tab_stubhead(label = "Subject")
# put a heading just above the column labels
gt_tbl <-
gt_tbl %>%
tab_header(
title = "Deconvolution against IP dataset using CIBERSORT v1.04 method"
)
gt_tbl <-
gt_tbl %>%
tab_source_note(
source_note = "Dataset provided by Zhang et al. 201641 and performed using BrainDeconvShiny"
)
# add another column title
gt_tbl <-
gt_tbl %>%
tab_spanner(
label = "Cell Type",
columns = c(Neurons, Astrocytes, Oligodendrocytes, Microglia, Endothelia)
)
# Show the gt Table
gt_tbl

Related

How do I define color groups based on numerical threshold values for ggplot2 barplot

I want to make a bar plot in ggplors where the value below zero is in red color. However, I got this graph instead of red color below zero value. Can anyone help me to solve the problem above? I have tried following some manual code but could not find the answers still.
Greatly appreciate for your help.
I tried this code:
Central_Java %>%
mutate(Category = factor(Category, levels = c("Minimum", "Medium", "Slightly Ideal", "Ideal"))) %>%
ggplot(aes(Province, Above_Below_Required_Staff, fill = Available_Staff, color=ifelse(Above_Below_Required_Staff>0, "red", "blue"))) +
xlab ("MPAs in Central Java\n(n=1)") + ylab ("Percent of Required Staff")+
ylim(-100,100)+
facet_wrap(~Category, ncol = 4)+
geom_bar(stat="identity")+
geom_hline(yintercept = 0, colour = "black") +
theme_light(base_size = 20) +
theme_bw()+
theme(text=element_text(size=19))+
ggtitle("Level of MPA Staff Capacity")+ theme(plot.title = element_text(hjust = 0.5))+
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)
'''
Best,
[enter image description here] (https://i.stack.imgur.com/HPvZe.png)

How to map discrete colours in a Plotly Sunburst chart in r

I am very new to using plotly in rstudio and have come up against a problem with mapping discrete colours (stored as hex codes in the field color) to each of the slices in my ids field.
I have included my code below:
df %>%
plot_ly(
color = I("black"),
marker = list(colors = ~color)) %>%
add_trace(ids = df$ids,
labels = df$labels,
parents = df$parents,
type = 'sunburst',
maxdepth = -1,
domain = list(column = 0)) %>%
layout(sunburstcolorway = df$color)
This is the resulting sunburst diagram I get using this code, which is obviously not ideal:
Ideally the first four levels would have the same colour, and then different hex colour codes are used for slices that are labelled "Poor","Moderate","GwC" or "Good".
A csv file of my data frame used above is available here.
I finally managed to nut out how to map my colour field to the background colours on the sunburst chart - have updated the code in original post. All that was required was to insert the following code segment:
plot_ly(
marker = list(colors = ~color))
Below is the output chart:

Create a table using Vpython

Use of the VPython library to create a small table in a simulated 3D space
this is what I've got
top = box(pos=vector(-1,4,4),
axis=vector(-1.5,0,-1.5),
size=vector(18,0.29,10),
up=vector(5,25,5),
color = color.blue)
leg1 = cylinder(pos=(-3,-3,0),
axis=vector(0,7,0), radius=0.45,
color = color.green )
leg2 = cylinder(pos=vector(3.5,-5,3),
axis=vector(0,9,0), radius=0.45,
color = color.green )
leg3 = cylinder(pos=vector(10.5,-3.9,2),
axis=vector(0,9,0), radius=0.45,
color = color.green )
leg4 = cylinder(pos=vector(-11.5,-6.5,-2.2),
axis=vector(0,11,0), radius=0.45,
color = color.green )
The first image is what I got and the second image is what I want, but turned upside down as a table would
Try drawing out the table top and legs on paper
The up of the table top should be default
The legs should have the same "length"
To have the legs point downwards use a negative y
If the center of your table is 0,0,0 then the legs will reflect each other's positions in reference to that.

How to specify dates on the x-axis to make a bar graph using Python 3

I want to make a bar graph just like the one below. How do I specify the dates on the x-axis using Python 3.5?
So I tried to replicate the axis values as much as possible in the below example.
Some properties you may need to know when setting the x-axis are:
type ( enumerated : "-" | "linear" | "log" | "date" | "category" )
default: "-" Sets the axis type. By default, plotly attempts to determined the axis type by looking into the data of the traces
that referenced the axis in question.
nticks (integer greater than or equal to 0)
default: 0 Specifies the maximum number of ticks for the particular axis. The actual number of ticks will be chosen
automatically to be less than or equal to nticks. Has an effect only
if tickmode is set to "auto".
tick0 (number or categorical coordinate string) Sets the placement of the first tick on this axis. Use with dtick. If the
axis type is "log", then you must take the log of your starting tick
(e.g. to set the starting tick to 100, set the tick0 to 2) except
when dtick="L" (see dtick for more info). If the axis type is
"date", it should be a date string, like date data. If the axis type
is "category", it should be a number, using the scale where each
category is assigned a serial number from zero in the order it
appears.
tickformat (string)
default: "" Sets the tick label formatting rule using d3 formatting mini-languages which are very similar to those in Python.
For numbers, see:
https://github.com/d3/d3-format/blob/master/README.md#locale_format
And for dates see:
https://github.com/d3/d3-time-format/blob/master/README.md#locale_format
We add one item to d3's date formatter: "%{n}f" for fractional seconds
with n digits. For example, "2016-10-13 09:15:23.456" with tickformat
"%H~%M~%S.%2f" would display "09~15~23.46"
tickangle (angle)
default: auto Sets the angle of the tick labels with respect to the horizontal. For example, a tickangle of -90 draws the tick
labels vertically.
So these properties are set to the below values.
type='date',
nticks=40,
tick0=86400000.0,
tickformat="%Y%m%d",
tickangle=-45,
Please checkout the below example and let me know if your issue is resolved!
import pandas as pd
import numpy as np
import plotly.offline as py_offline
import plotly.graph_objs as go
py_offline.init_notebook_mode()
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
trace_high = go.Bar(
x=df.Date,
y=df['AAPL.High'],
name = "AAPL High",
opacity = 0.8)
data = [trace_high]
layout = dict(
title = "Manually Set Date Range",
xaxis = dict(
type='date',
nticks=40,
tick0=86400000.0,
tickformat="%Y%m%d",
tickangle=-45,
range = ['2016-07-01','2016-07-31'])
)
fig = dict(data=data, layout=layout)
py_offline.iplot(fig, filename = "Manually Set Range")
If your data are in a dataframe accompanied with a date column ('dat') you first need to make that column a date object and then make it an index.
df['dat'] = pd.to_datetime(df.['dat'])
df.index = pd['dat']
df['column name'].plot()
you can also sort your values before plotting
df.sort_value('dat')

Web scraping using rvest

I want to scrape all the text in the following website:
http://curia.europa.eu/juris/document/document.jsf?text=&docid=49703&pageIndex=0&doclang=en&mode=lst&dir=&occ=first&part=1&cid=656172
My code:
html = http://curia.europa.eu/juris/document/document.jsf?text=&docid=49703&pageIndex=0&doclang=en&mode=lst&dir=&occ=first&part=1&cid=656172
main_content <- html_nodes(html, css = "#document_content")
main_text <- main_content %>% html_nodes("p") %>%html_text()
However, in this way, not all the text are extracted because some text is in the node "dd"..."/dd"
I wonder if I can do something like html_nodes("p") or html_nodes("dd") or html_nodes("dt") to replace html_nodes("p") in the above dode.
How can I achieve this? Or is there any other way I can accomplish my task? Ideally, I dont want to use
main_text <- main_content %>% html_text()
because I want to separate each sentence.
When selecting css, if you separate the nodes you want by a comma, that is like a logical OR...
library("rvest")
url = "http://curia.europa.eu/juris/document/document.jsf?text=&docid=49703&pageIndex=0&doclang=en&mode=lst&dir=&occ=first&part=1&cid=656172"
page <- read_html(url)
main_text <- page %>%
html_nodes("#document_content") %>%
html_nodes("p,dd,dt") %>%
html_text()

Resources