I was trying to generate an r markdown html/pdf docuement and everything worked apart from the code and images didn't appear. What I discovered is that images don't appear if you have two that are the same. In the example below you only get 2 plots, event though 4 are asked for. If you give them all different titles (main='xx') then they all appear.
Is this a known issue that could be causing my problems? Any advice appreciated.
```{r pressure, echo=TRUE}
cat('HELLO')
plot(pressure) # only one of these plots bu it seems to be random
cat('HELLO1')
plot(pressure) #
cat('HELLO2')
plot(pressure) #
cat('HELLO3')
plot(pressure)
plot(pressure,main='plot3') #this one plots
Related
I want to do a graph by group. For histograms, that can be done with the option hist ... , by(group). Unfortunately, stata will attempt to fit all groups on the same figure. This becomes unreadable when there is many groups.
I'm looking for a solution that will allow me to fix the number of subplots per figure, and create multiple figures -- but I'd also be open to alternative solutions that are "industry standard". I'll try to make the solution work for a multitude of figure types, not only hist, and appreciate approaches that dont rely on additional packages.
Here's a sample dataset, where -- for example -- I'd like to fix that no more than 4 groups are shown per figure.
sysuse educ99gdp
hist public, by(country)
I don't have any fixed idea on how exactly this should look, it should be scalable for 30-50 categorical values though. So, grouping the countries and showing multiple histograms in the same subplot works fine when there's only 10, not so fine when there is 30.
One suggested solution - but I'm happy to have it another way that is more Stata-ish - would be to have 3 separate figures, 4 subplots on each, and the last figure would have two empty slots.
Initially, I created an interactive map of the UK Postcode area where an individual area is color represented based on its value (e.g. population in that post code area) as following.
from bokeh.plotting import figure
from bokeh.palettes import Viridis256 as palette
from bokeh.models import LinearColorMapper
from bokeh.models import ColumnDataSource
import geopandas as gpd
shp = 'file_path_to_the_downloaded_shapefile'
#read shape file into dataframe using geopandas
df = gpd.read_file(shp)
def expandMultiPolygons(row, geometry):
if row[geometry].type = 'MultiPolygon':
row[geometry] = [p for p in row[geometry]]
return row
#Some rows were in MultiPolygons instead of Polygons.
#Expand MultiPolygons to multi rows of Polygons
df = df.apply(expandMultiPolygons, geometry='geometry', axis=1)
df = df.set_index('Area')['geometry'].apply(pd.Series).stack().reset_index()
#Visualize the polygons. To visualize different colors for different post areas, I added another column called 'value' which has some random integer value.
p = figure()
color_mapper = LinearColorMapper(palette=palette)
source = ColumnDataSource(df)
p.patches('x', 'y', source=source,\
fill_color={'field': 'value', 'transform': color_mapper},\
fill_alpha=1.0, line_color="black", line_width=0.05)
where df is a dataframe of four columns : post code area, x-coordinate, y-coordinate, value (i.e. population).
The above code creates an interactive map on a web browser which is great but I noticed the interactivity is not very smooth in speed. If I zoom in or move the map, it renders slowly. The size of the dataframe is only 1106 rows, so I'm quite confused why it is so slow.
As one of the possible solutions, I came across with datashader (https://datashader.readthedocs.io/en/latest/) but I find the example script is quite complicated and most of them are with holoview package on Jupyter notebook but I want to create a dashboard using bokeh.
Does anyone advise me in incorporating datashader into the above bokeh script? Do I need a different function within datashader to create the shape map instead of using bokeh's patches function?
Any suggestion would be highly appreciated!!!
Without the data file involved, I can't answer your question directly, but can offer some observations:
Datashader is unlikely to be of value for this purpose, because datashader does not currently have any support for rendering polygons. As a rule of thumb, Datashader is designed to aggregate your data, and if it's already aggregated, Datashader won't normally be of help. Here your data is aggregated by postcode, which datashader can't process, but if you had the original data per person it would be happy to render it.
If you prefer working with Bokeh directly rather than via the higher-level HoloViews/GeoViews interface, I'd recommend folllwing Matt Rocklin's work on accelerating geopandas; his approach should be very fast for your purpose.
All that said, HoloViews, and GeoViews should be a convenient way to work with Bokeh in general, whether or not you want to create a dashboard. E.g. the 2017 JupyterCon tutorial shows how to make a simple Bokeh dashboard using both libraries. It doesn't cover shape files, but those are covered in other GeoViews examples.
As mentioned in my comment, I believe that the complexity of your polygons might cause your problem. The file you linked to contains several shapefile of different sizes and complexities. You can simplify those, i.e. reduce the number of points for each polygon. This can change how they look. It can range from almost no difference over a bit more "edginess" to an angular appearance. This depends on the level of simplification you chose. Depending on your needs you can chose different levels of simplicity.
I know of three easy options to get this done:
GUI: Try QGis. It is a great opensource tool for geospatial data processing. Load your Shapefile as a new layer. Then use the "Simplify Geometries" tool under the Vector menu.
Command-Line: GDAL is an open-source library. It comes with an useful command-line tool. You can use it like this: ogr2ogr outfile.shp infile.shp -simplify 0.000001
Online: Visit mapshader. Import your file. Select simplify and chose your level. Then, export the result. What I really like here is that your file is rendered instantly. Hence, you can immediately see the result of your simplification.
Other than that, you should also update your bokeh version. It gets updated regularly and there have been some performance improvements since.
Using HoloViews or GeoViews will not positively affect your performance. Thus, it is not related to your issues. I guess #James A. Bednar was just giving some side advice there.
I found a way to speed up the interactive visualization of the UK map as I move the slider.
I created individual image (in 2D) for a different value of slider first and updated the map using the 2D images instead of using bokeh's patches function.
Since the images are in array format, it is much faster to update the image while changing the values in the slider. one downside in this method is that I can no longer use hover function on the UK map.
I referred to the following url to convert polygon information into arrays: https://gist.github.com/brendancol/db030013e981c46acb2886060dde607e#file-rasterio_datashader_polygons-py-L35
I've seen some answers that are tangentially related to my question but they're sufficiently different that I'm still not sure how to go about this. I have a pivot table in Python organized as follows:
and I would like to create a bar plot where on the x-axis I have 6 sections for each of the 6 departments (A,B,C, etc.) and then for each I have two bar plots that show the value in the "Perc Admitted" column for Males and Females, with them colored differently for clarity. I've been trying to use Seaborn's barplot for this, but cannot seem to get it to come together, i.e. trying for example
sns.barplot(data = admit_data, x = 'Dept', y = 'Perc Accepted', hue = 'Gender')
gets me an "ValueError: Could not interpret input 'Dept'" error. Still learning Python and not sure how to best do this...any suggestions would be greatly appreciated. I'm also open to using matplotlib.pyplot or other library if that also provides for an elegant solution. Thank you!
I am writing up a lesson in HTML using rmarkdown to demonstrate how to implement analytic methods in R, and because of this the document has a lot of code that is needed to understand those methods, but also a lot of code that is used only for generating plots and figures. I would like to show the first sort of code by default, and leave the plotting code available for students to view but hidden by default.
I know that rmarkdown has recently added support for code folding by setting the code_folding html_document argument to either show or hide. However, this either leaves all code chunks unfolded or folded by default -- is there any way to indicate whether individual code chunks should be shown or folded by default while allowing code folding?
Thank you!
I arrived here wondering the same thing. This is a not a perfect solution, but I write the code twice: once in regular markdown (so it displays - note no {r} after the three backticks), and another time in a code chunk (so it runs).
Example:
This runs but doesn't display the actual code
```{r}
5 * 5
```
This results in both the code and execution being displayed
```
5 * 5
```
```{r}
5 * 5
```
Which results in:
David Fong provided a perfect solution for this in their answer: https://stackoverflow.com/a/56657730/9727624
To override the state, use {r class.source = "fold-hide"} if the yaml setting is show, and {r class.source = "fold-show"} if the setting is hide.
I am developing an application in R Shiny. One of my modules in the application, displays dynamic text depending on user inputs. I would like to display the text as bullet points and additionally would like the text in "strong" or "heading" format. I can do this individually but somehow I am not able to figure out how to combine the 2. So assuming that my function returns a character vector a, with 2 components that I need to display, I can do the following:
HTML("<ul><li>",a[1],"</li><li>",a[2]) #To generate bullet points
HTML(paste(h4(a[1],a[2],sep=''))) #To concatenate and change format to heading
Now my question is - How can I do both (display as bullets with heading format)? I tried different combinations but it's not working.
Secondly, how do I change the colour of my text?
Any help will be greatly appreciated.
Thanks!
To get a bullet list with heading format, you can just add an <h4> tag to the paste. Also, make sure you close all the tags you open, for ex:
HTML("<ul'><li><h4>",a[1],"</h4></li><li><h4>",a[2],"</h4></li></ul>")
You can use inline CSS to change the color, for ex:
HTML("<ul style='color:red'><li><h4>",a[1],"</h4></li><li><h4>",a[2],"</h4></li></ul>")
More info here.